Greetings…I am not an entire noob in regards to C# and Prefab creation. However, i feel i may be over looking something in my scripting that may seem to be an obvious error. Here is the problem:
I have a Prefab (Tank) that i set with an enterExitTank script assigning various Components that control both camera actions, turret view, movement, ballistics and player activity. All of this is working perfectly without errors. Where the issue remains is when i create multiple tanks on a map and attempt to enter them.
For example, the Player approaches tank prefab copy #3 during play, attempts to enter vehicle #3 but ends up inside tank prefab camera and controls of Tank prefab #1. Is there a way to maintain Component controls to keep the scripts dedicated to the prefab copy…not the original?
As I stated before…this may be a minor oversight. Thank you.
All of the enterExitTank script instances will run their Update() function on the same frame, one after the other. The first one that runs will check if isPlayerVisible is true and if the enter-tank key has been pressed. If so, it will act immediately, regardless of any of the other enterExitTank scripts, and regardless of player location. As soon as the second script instance runs its Update function, isPlayerVisible is now false, because the player has entered the first tank.
What you need is a third check, to see if the player is close to the tank that the script is attached to. Perhaps the easiest way is to check that Vector3.Distance(player.position, exitPoint.position) is less than a desired amount. So if the player is visible, the key is pressed, and the player is close enough to the exit point, then you can enter that particular tank. If you’re too far away, you won’t enter that tank, and the next tank’s enterExitTank script will have a chance to do its own checks.
Note that this will also prevent you from entering the tank from magically far away, even if there’s only a single tank. In testing, you may have always been running up to the tank before pressing E, but your code suggests that there was no actual need to do so.
i think you misunderstand the problem…the problem is:
there are three tanks on the field (Tank1, Tank2 and Tank3). When laying the prefab on the field they make ‘copies’ of the original prefab first placed (Tank1, Tank1(1), Tank1(2)…etc.). when the player enters either Tank1(1) or Tank1(2) it places the player in Tank1 and allows camera/movement controls for that prefab. Not the one entered.
Oh right, sorry. I missed your trigger enter/exit. I was stupidly imagining that isPlayerVisible was a global or something.
So your code looks fine, but I can’t see enough in your images to see if everything in the hierarchy and inspector is good. It looks like the vehicle field has been overridden from the prefab default, but I can’t see if it has the correct reference.
So it sounds like you might have incorrect object references assigned in the inspector, and so when playing, you end up assigning the player to the wrong parent. What does the hierarchy look like once you’ve begun playing and tried to enter the second or third tank?