Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bugs with jump, please help

Discussion in 'Scripting' started by BigEthano, Apr 17, 2014.

  1. BigEthano

    BigEthano

    Joined:
    Apr 17, 2014
    Posts:
    1
    this is my script, just add it on to a cube (that has rigidbody on it), its on c# by the way-

    using UnityEngine;
    using System.Collections;

    public class MovingPlayer : MonoBehaviour {

    public float moveSpeed = 5f;
    public float turnSpeed = 150f;
    public int jumpHeight = 5;

    void Update () {
    if(Input.GetKey(KeyCode.W))
    transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
    if(Input.GetKey(KeyCode.S))
    transform.Translate (Vector3.forward * -moveSpeed * Time.deltaTime);
    if(Input.GetKey(KeyCode.A))
    transform.Rotate (Vector3.up * -turnSpeed * Time.deltaTime);
    if(Input.GetKey(KeyCode.D))
    transform.Rotate (Vector3.up * turnSpeed * Time.deltaTime);
    if(Input.GetKey(KeyCode.Space))
    transform.Translate (Vector3.up * jumpHeight * Time.deltaTime);
    }
    }
     
    Last edited: Apr 17, 2014
  2. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    If your cube has a rigidbody on it, try applying it a force instead of moving the transform. This way you'll have the physics engine doing the calculations for you.

    If you don't have/want a rigidbody on your cube, then you have to do the calculation yourself.

    Jump happens over time, with one press and not holding (usually at least) so when the user presses the jump button, you do not translate it but instead you could initialize a timer and for example you translate up for half the timer and translate down for half the other (that's just a hint as to how to do it, you'll figure out what best fits your needs if you don't want to do it with the rigidbody...)