- Home /
problem with the jump code..
iam learning eteeski's fps1.5 jumping the right way game tutorial in which, my player has to jump when i press space bar.the code for jumping is as follows,
var walkacceleration : float;
var cameraobject : GameObject;
var maxwalkspeed : float = 20;
@HideInInspector
var horizontalmovement : Vector2;
var jumpvelocity : float = 20;
@HideInInspector
var grounded : boolean = false;
var maxslope : float = 60;
function Start () {
}
function Update () {
horizontalmovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
if(horizontalmovement.magnitude > maxwalkspeed )
{
horizontalmovement = horizontalmovement.normalized;
horizontalmovement *= maxwalkspeed;
}
rigidbody.velocity.x = horizontalmovement.x;
rigidbody.velocity.z = horizontalmovement.y;
transform.rotation = Quaternion.Euler(0 ,cameraobject.GetComponent(lookupscript).currentyrotation, 0);
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkacceleration, 0 , Input.GetAxis("Vertical") * walkacceleration);
**if(Input.GetButtonDown("Jump"))
rigidbody.AddForce(0,jumpvelocity,0);**
}
the problem is my player is not jumping when i press my spacebar. can any one help me.
Answer by AlucardJay · Jan 06, 2013 at 07:49 AM
Force is applied over time, but your jump code only applies the force when the spaceBar is pressed. To use force like this (triggered once but affect the rigidbody properly), you need to use ForceMode.Impulse
eg :
if(Input.GetButtonDown("Jump")) {
rigidbody.AddForce( Vector3(0,jumpvelocity,0), ForceMode.Impulse );
}
http://docs.unity3d.com/Documentation/ScriptReference/ForceMode.Impulse.html
http://docs.unity3d.com/Documentation/ScriptReference/ForceMode.html
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html
thank u jay kay for ur valuable help, its working now. but in the tutorial wat iam studyin this is the code the tutor used and it worked.
not sure then, but glad it worked. Can you link the tutorial? I shall have a look.
gagaboy : here is the link for the tutorial : http://www.youtube.com/watch?v=eSnzuRgrN_8&list=PL7AE076AFAFD3C305
$$anonymous$$e : it looks like you didn't finish the video. Go to 21:30, there is more code you havn't got in your question, eg
if ( Input.GetButtonDown("Jump") && grounded )
and then the collision function, etc etc. I suggest finishing the course, as changing the code will have strange results.
Your answer
Follow this Question
Related Questions
Unity Editor Crashes On Play Because of Import Script 1 Answer
Script Error with CharacterMotor 1 Answer
How can I reduce speed of an object, the more that object gets close to target? 1 Answer
How to use a controller button and keyboard key? 0 Answers
Making a character jump and crouch using a Joystick 1 Answer