- Home /
Glitchy Movement on Slopes using Character Controller
I am using a character controller with a modified version of the PlayerController.js script. They player isn't able to move regularly on terrain based slopes. If your moving up and attempt to turn around, the players rotation isn't updated until x amount of time,
Here is the block that moves the player:
// We store speed and direction seperately,
// so that when the character stands still we still have a valid forward direction
// moveDirection is always normalized, and we only update it if there is user input.
if (targetDirection != Vector3.zero)
{
//If we are really slow, just snap to the target direction
if (moveSpeed < walkSpeed * 0.9 && grounded)
{
moveDirection = targetDirection.normalized;
}
// Otherwise smoothly turn towards it
else
{
moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
moveDirection = moveDirection.normalized;
}
}
Now, When I get rid of the if (targetDirection != Vector3.zero)
condition and everything else except this:
moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
moveDirection = moveDirection.normalized;
the player is then able to move around smoothly on the hills, except now the player's character model's orientation doesn't update correctly.
Any suggestions?
Answer by Statement · Apr 01, 2011 at 12:34 PM
Except now the player's character model's orientation doesn't update correctly.
Well, you haven't posted any code regarding your orientation. Maybe you could try something like:
transform.LookAt(transform.position + moveDirection);
or
transform.LookAt(transform.position + moveSpeed);
at some place in the code which is always run after your movement code have run.
Now it doesn't update unless keys are pressed (meaning you have to let go and press again for it to update.) I pasted my $$anonymous$$ovement function above. I didn't want to paste my controller script, for it is way to big. :D
Note: You could always paste to pastebin.com for large files
Eh, I just realized how stupid it was of me to write transform.LookAt(transform.position + transform.forward);
:) If course that's where the transform already is looking. D'oh.
Your answer
Follow this Question
Related Questions
Turning a rigidbody controller into a character controller (almost) 1 Answer
3D Character Controller slowing down the higher the slope angle (both up and down) 1 Answer
Non slippery movement 1 Answer
How do you calculate DeltaX and DeltaZ for a character? 1 Answer
Movement with AddForce: Wrong Direction 2 Answers