So I created a script for a football. Whenever the football enters the trigger region of the player and the player presses E then the football will be kicked. But for some reason, the script only works on trigger but when I press E the ball is not kicked. Please Help! The script is below:
The problem with your code is that you are attempting to get input on the same frame as when the trigger occurs. - Just 1 frame of opportunity*
Try to re-work your script/logic so that you can determine when inside the trigger is valid, and check for the input in your Update() loop when that’s true. See how that goes
Firstly, put your code inside the code tag (either manually or by clicking the ‘insert’ button on the toolbar where you type the post then click code). Makes it much easier to read.
Secondly the reason it isnt working as you expect is OnTriggerEnter is only called the first frame the ball enters the trigger. This means your code is only run on one frame - if you press E on that exact frame, then it would work. Otherwise if the ball is sitting in the trigger, that code is not run.
The naive solution short term is use OnTriggerStay instead however this has issues (A lot of unnecessary tag comparisons and tying input to the physics update). You could try this to test out your logic but a more robust solution depends on how you implement the game.
I am assuming that you have a permanent reference to the ball (football variable)? If so then you can use OnTriggerEnter and Exit to set a boolean flag (eg. hasBall) to true (Enter) or false (Exit). Then in your update you can process input if hasBall is true.
Glad to hear it works.
As per my previous post though, you should look to use alternative logic (such as a flag) as there are issues with input processing etc. Physics updates run at a different rate (slower than update under normal circumstances) so you want to avoid input handling (The OnCollision/OnTrigger events are from the physics system).
I agree, the flag sounds better. Plus, you could still miss an input in OnTriggerStay. Mind you, the chance is much smaller than OnTriggerEnter, but it could happen.