- Home /
Question by
ExtremePowers · Dec 26, 2014 at 06:52 PM ·
animationgravitycharacter controllerstuck
Character controller problem
I have a problem with my code that the animations is locked at their finish state and isGrounded is never true after the first time the player moves. It also continues to go in one direction.
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
isSliding = false;
var hit1 : RaycastHit;
if(Physics.Linecast(transform.position, -Vector3.up , hit1, 2.5) ) {
var cosAngle = Vector3.Dot(Vector3.up, hit1.normal);
if(cosAngle <= slideAngle) {
isSliding = true;
var moveSpeed = (1 - cosAngle)*speed;
//The steeper the hill, the faster we slide.
moveDirection = new Vector3(hit1.normal.x , -hit1.normal.y , 0);
moveDirection = moveDirection.normalized;
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= moveSpeed;
}
anim.SetBool(FallHash, true);
}
if (!isSliding) {
var j = 0;
if (Input.GetButton("Jump")) {
j = jumpSpeed;
}
moveDirection = Vector3(Input.GetAxis("Horizontal"), j, Input.GetAxis("Vertical"));
if (moveDirection.x != 0 || moveDirection.z != null) {
anim.SetFloat(SpeedHash, 1);
} else {
anim.SetFloat(SpeedHash, 0);
}
moveDirection = transform.TransformDirection(moveDirection);
anim.SetBool(SprintHash, Input.GetButton("Sprint"));
if (Input.GetButton("Sprint")) {
moveDirection *= sprintSpeed;
} else {
moveDirection *= speed;
}
anim.SetBool(FallHash, false);
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
Comment
Don't just post a block of code and ask people to check it. Look over the code yourself and ask about anything you don't know how to do, or at least give us a narrower piece of code to work with.