Search Unity

Jumping with forces

Discussion in 'Scripting' started by darthbator, Sep 16, 2012.

  1. darthbator

    darthbator

    Joined:
    Jan 21, 2012
    Posts:
    169
    Hey there gents! I had a really quick simple question about jumping via forces. I was prototyping a simple runner type game and ran into some issues adding jumping. Here is what I am working with

    Code (csharp):
    1. void Update () {
    2.         Vector3 myPos = transform.position;
    3.         myPos.z = zLock;
    4.         transform.position = myPos;
    5.         //if (Input.GetKeyUp(KeyCode.Space)) rigidbody.AddForce(Vector3.right * forceMulti);
    6.         rigidbody.AddForce(Vector3.right * forceMulti);
    7.         if (Input.GetKeyDown(KeyCode.Space)) {
    8.             rigidbody.AddForce(Vector3.up * jumpMulti);
    9.             Debug.Log("I'M JUMPING OMG!!");
    10.         }
    11.     }
    I get the debug print for the jump but the Y position of the character is basically totally constant. Thanks for the help!
     
  2. boco

    boco

    Joined:
    Dec 7, 2010
    Posts:
    373
    use Rigidbody.velocity instead you will get your desired result also you will need to have a function that detects if your character is touching the ground otherwise this will be a flying script :D
     
  3. darthbator

    darthbator

    Joined:
    Jan 21, 2012
    Posts:
    169
    WEEEEEEEEEE FLYING!!!! I'll probably end up creating a state system for grounding and etc. I just wanted to kinda get a basic model of a object flying forward first. Thanks!
     
  4. darthbator

    darthbator

    Joined:
    Jan 21, 2012
    Posts:
    169
    I figured I would just come back in and say that the issues was with the forcetype I was trying to apply. Using the default force was not sufficient to accelerate the object. One I change the force type to impulse I had jumping!