- Home /
How to force a player to jump?
I am using the FPS Controller that comes with Unity. I want the player to jump (out of their control) when something collides with them. I have everything in place except for going about coding the line of code that would force the jump.
In JavaScript is there away to access the character motor in another script to trigger the jump with no input from the player?
Thank you in advance!
Answer by Tehnique · Jul 23, 2014 at 08:36 PM
Yes, you can modify the controller script and expose the method that triggers the jump (the one called when Space is pressed). Just use GetComponent on your FPS Controller to get the script with the method, and call it.
Thank you, it works! I probably jumped the gun and preemptively posted this, I have been trying alternatives to a particular mechanic and they have all been failing. I was getting desperate. Haha!
Please mark question as answered so it can be closed. Thank you.
Answer by contab009 · Jul 24, 2014 at 09:49 AM
You can also directly modify the velocity vector of any gameobject, even if it isn't the first person controller that comes with Unity or if it doesn't have any method to trigger the jump. The line of code would look something like this :
rigidbody.velocity = new Vector3(rigidbody.velocity.x, jumpforce, rigidbody.velocity.z);
The jumpforce being a variable (preferably public) so that you can alter it later.
Your answer
Follow this Question
Related Questions
How to position the jump animation with the object's body? 1 Answer
Why won't this jump code work? 1 Answer
Jump Script Not working (3D) 2 Answers
Jumping not always work 2 Answers
Box collider didn't jump with player 1 Answer