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.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
//Variables
public GameObject player;
public float MaxDistance = 5.0f;
//Acceleration of gravity
public float Acceleration = -9.81f;
public float VelocityFinal = 0.0f;
float Time1 = 0.0f;
float previousDistance = 0.0f;
void Update ()
{
Time1 += (float)(Time.deltaTime);
JumpCalc(Time1);
}
void JumpCalc(float time)
{
float VelocityInitial = Mathf.Sqrt(-2f * Acceleration * MaxDistance);
float CurrentDistance = (VelocityInitial * time) + (-2f * Acceleration * time * time);
transform.Translate (Vector3.up * (CurrentDistance-previousDistance) * Time.deltaTime);
previousDistance = CurrentDistance;
}
}
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’.