- Home /
Trying to Add jump to this code.
Hey guys, I got this code from the unity script reference and I've been trying to add Jump function to it, I cannot work out how. I tried using the adforece.up and also tried to use the other move direction from another reference i couldnt work it. Ive also looked everywhere online but cant find a way to do it, can anyone help me with this please? using UnityEngine; using System.Collections;
public class Player : MonoBehaviour {
public float speed = 10.0f;
public float rotationSpeed = 100.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
void Update() {
float translation = Input.GetAxis("Vertical") * speed;
float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate(0, 0, translation);
transform.Rotate(0, rotation, 0);
}
}
Take a look at the sample script for CharacterController.move. The logic is very similar to what you are trying to do, and it implements gravity. You will have to provide your own definition of 'isGrounded'. That may be based off of 'Y' height or you could use a raycast.
Answer by InfiniBuzz · Jul 01, 2013 at 03:35 PM
Hi!
I assume that you are not using a character controller ;) you probably did not apply a force, big enough to move the object. Also use ForceMode.Impulse
public class Player : MonoBehaviour {
public float speed = 10.0f;
public float rotationSpeed = 100.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public float jumpForce = 10000.0f; // This will be a very high jump ;P
// or jumpSpeed (see below)
void Update() {
float translation = Input.GetAxis("Vertical") * speed;
float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate(0, 0, translation);
transform.Rotate(0, rotation, 0);
// -
if(Input.GetKeyDown(KeyCode.Space))
{
rigidbody.AddForce(transform.TransformDirection(Vector3.up) * jumpForce, ForceMode.Impulse);
}
}
}
you can also set a velocity to the rigidbody:
rigidbody.velocity += transform.TransformDirection(Vector3.up) * jumpSpeed;
note that you set jumpSpeed
to something around 5-10 while jumpForce
needs to be a much bigger value.
Hey! thanks for that, the jump seems to work fine now, the only problem is now after you jump and try to rotate left or right you end you doing a flip and turn up upside down, any idea if there is a solution for that?
Never $$anonymous$$d fixed it, have to freeze the x and z. Thanks heaps for your help :)
ok, as mentioned in robertbu's comment above, you should think about using a charactercontroller if you want to extend this.
Your answer
Follow this Question
Related Questions
Character Controller slides sideways when it hits objects are angles different from 90 degrees 1 Answer
Rotation used as movement 4 Answers
Joystick for WASD & Joystick for Camera? 0 Answers
3D Heat Seeking Missile (C#) - Odd problem 1 Answer
How to keep player character on top of a Rolling game object? 1 Answer