i am making a simple 2d game and am using a character controller for my movement. i want the character to have a smooth jump but the script im using makes it instantly jump up and slowly fall back down. how can i make the jump smooth? this is the script im using:
var jump : int = 10;
function Update()
{
if(Input.GetButtonDown("Jump") && controller.isGrounded)
{
transform.Translate(0, jump * Time.deltaTime, 0);
}
}
3 Answers
3
Use .Move or .SimpleMove when using a Character Controller. Using the example code in Unity - Scripting API: CharacterController.Move you would do this to jump:
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
void Update() {
if (controller.isGrounded && Input.GetButton("Jump")) {
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
How does that work? It’s setting the moveDirection’s Y instantly. How can it possibly be smooth?
The code that @mattssonon provided gets really glitchy when you collide with an object. How do I fix that?
See here http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.Move.html the script has it all, movement and jump.
– fafasehow do i do this without the movement parts?
– onionman99