- Home /
Question by
Shirogatsu · Nov 17, 2012 at 03:25 PM ·
movementgravitycharacter controller
Problem with gravity and character controller
Hello, i've bumped into little problem.
void Update(){
if(!mvmControl.isGrounded)
moveDirection.y-=5; //gravity
if(mvmControl.isGrounded) {
moveDirection = newVector3(0,0,Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
}
mvmControl.Move(moveDirection*Time.deltaTime);
}
My mvmControl.isGrounded condition is always flickering true/false and i can't ground my character :( With .SimpleMove everything is ok, but i want to use .Move for more control
Comment
try checking the skin width and the $$anonymous$$ move distance of your controller, you should also have gravity as an acceleration for a much more realistic effect.
Answer by Yofurioso · Dec 10, 2013 at 01:01 PM
Try this:
private float y;
private float z;
private Vector3 moveDirection;
void Update()
{
if(!mvmControl.isGrounded)
{
y -= 5f; //gravity
}
if(mvmControl.isGrounded)
{
if(y < -0.1f)
{
y = -0.1f;
}
z = Input.GetAxis("Vertical");
}
moveDirection = new Vector3(0, y, z);
moveDirection = transform.TransformDirection(moveDirection);
mvmControl.Move(moveDirection * Time.deltaTime);
}
That way, even if the character controller is grounded you will be applying a small downwards movement, forcing it to stay grounded.
Your answer
![](https://koobas.hobune.stream/wayback/20220613084025im_/https://answers.unity.com/themes/thub/images/avi.jpg)