Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Jump help.

Discussion in 'Scripting' started by Brandon-Keeler, Nov 18, 2014.

  1. Brandon-Keeler

    Brandon-Keeler

    Joined:
    Oct 27, 2014
    Posts:
    75
    I have a player with a kinetic rigidbody. I am trying to simulate gravity, and make this character jump, like it would if it were under unitys gravity. However when I try this, my character never falls.

    This is what I have tried.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class PlayerController : MonoBehaviour
    4. {
    5.     //Variables
    6.     public GameObject player;
    7.  
    8.     public float MaxDistance = 5.0f;
    9. //Acceleration of gravity
    10.     public float Acceleration = -9.81f;
    11.     public float VelocityFinal = 0.0f;
    12.     float Time1 = 0.0f;
    13.     float previousDistance = 0.0f;
    14.  
    15.     void Update ()
    16.     {
    17.         Time1 += (float)(Time.deltaTime);
    18.         JumpCalc(Time1);
    19.     }
    20.  
    21.     void JumpCalc(float time)
    22.     {
    23.         float VelocityInitial = Mathf.Sqrt(-2f * Acceleration * MaxDistance);
    24.         float CurrentDistance = (VelocityInitial * time) + (-2f * Acceleration * time * time);
    25.         transform.Translate (Vector3.up * (CurrentDistance-previousDistance) * Time.deltaTime);
    26.         previousDistance = CurrentDistance;
    27.     }
    28. }
    29.  
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You always go up due to your current calculation, (CurrentDistance-previousDistance) > 0 at any given time.
    That means you'll move ( a value > 0) * Time.delteTime in up direction.

    That's because 'velocityInitial' always has the same value as long as you don't change 'Acceleration' and 'MaxDistance', whereas 'time' just grows. Therefore, 'CurrentDistance' will always be greater in every next Update. As you put the value from CurrentDistance into 'previousDistance' to save it for the next Update (where the CurrentDistance will be greater than this Update), you always have the situation 'CurrentDistance > previousDistance'.
     
  3. Brandon-Keeler

    Brandon-Keeler

    Joined:
    Oct 27, 2014
    Posts:
    75
    yeah i figured out the problem

    Code (CSharp):
    1. float CurrentDistance = (VelocityInitial * time) + (-2f * Acceleration * time * time);
    the -2f should have been 0.5f

    After testing this code, it still doesnt look like unity's gravity on non-kinesmatic objects. Anybody know what I am doing wrong? This is my new code.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     private float MaxDistance = 9.81f;
    7.     private float Acceleration = -9.81f;
    8.     public float VelocityFinal = 0.0f;
    9.     float Time1 = 0.0f;
    10.     float previousDistance=0.0f;
    11.  
    12.     void Update ()
    13.     {
    14.         Time1 += (float)(Time.deltaTime);
    15.         JumpCalc(Time1);
    16.     }
    17.     void JumpCalc(float time)
    18.     {
    19.         float VelocityInitial = Mathf.Sqrt(VelocityFinal - (2f * Acceleration * MaxDistance));
    20.         float CurrentDistance = (VelocityInitial * time) + (0.5f * Acceleration * time * time);
    21.         transform.Translate (Vector3.up * (CurrentDistance-previousDistance) * Time.deltaTime);
    22.         previousDistance = CurrentDistance;
    23.     }
    24. }