I am using tilemap and character controller. (Thanks for advice. I changed rigidbody to character controller.)
When I press arrow key, my character walks up-ward slope well. (natural walking on downhill is also problem. )
but when I pop the arrow key on the slope, my character suddenly jump once.
when I change runspeed, jumping height is also changed. I don't know why.
can anybody help me?
CharacterController controller;
float runSpeed = 20.0f; // run speed
float maxRunSpeed = 20.0f;
float gravity = 15.0f;
float jumpForce = 10.0f;
Vector3 vVelocity = new Vector3(0, 0, 0);
void Start () {
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
MoveHorizontal();
}
//-------------------------------------------------------------------------------------------
// Movement of Left / Right direction of Character
//-------------------------------------------------------------------------------------------
int MoveHorizontal() {
float ctrlX = Input.GetAxis("Horizontal"); // Get Input
float ctrlY = Input.GetAxis("Vertical");
bool ctrlJump = Input.GetButtonDown("Jump");
vVelocity.x = ctrlX * runSpeed;
vVelocity.y = controller.velocity.y - gravity * Time.deltaTime;
if( ctrlJump == true ) {
vVelocity.y = jumpForce - gravity * Time.deltaTime;
}
controller.Move(vVelocity * Time.deltaTime );
return 0;
}