Y value in transform.Translate when not grounded - gravity problem
I am moving my object with transform.Translate. One condition i have in my Update method is to check if the object isGrounded, like this:
if (IsControllerGrounded())
{
verticalVelocity = 0;
}
else
{
verticalVelocity = gravity;
}
What is not working is the "gravity" variable. This is the complete code:
private CharacterController controller;
float moveSpeed = 6;
float gravity;
float verticalVelocity;
float jumpVelocity;
float velocityXSmoothing;
void Start()
{
controller = GetComponent<CharacterController>();
gravity = -1f;
//jumpVelocity = Mathf.Abs(gravity) * timeToJumpApex;
}
void Update()
{
if (IsControllerGrounded())
{
verticalVelocity = 0;
}
else
{
verticalVelocity = gravity;
}
Debug.Log(verticalVelocity);
if (MobileInput.Instance.SwipeIzquierda)
{
gameObject.transform.Translate((-1 * Time.deltaTime * moveSpeed), verticalVelocity * Time.deltaTime, 0);
}
if (MobileInput.Instance.SwipeDerecha)
{
gameObject.transform.Translate((1 * Time.deltaTime * moveSpeed), verticalVelocity * Time.deltaTime, 0);
}
}
When i start the game i have the object like this:
So the object is supposed to fall to the ground, because at the beggining is not grounded. But this is not happening, the object stays up there. In the console if i log the "verticalVelocity" variable i can see that is correctly set to -1f.
Am i missing something in the Translate method?
Thanks
Your answer
Follow this Question
Related Questions
How can I make this translation smoother? 0 Answers
Increase velocity of gameObject every time it spawns? 1 Answer
Transform constant moving in scene even if translated for Vector3.zero 1 Answer
How do I specify the origin and end points of the arc1 instantiated line? 0 Answers
transform.Translate is too smooth 3 Answers