My problem in ONE gif:
GIF HERE
MovementScript
#pragma strict
var speed : float = 5.0;
var realSpeed : float;
var grounded : boolean;
var isCrouch : boolean;
var canRun : boolean = true;
var canCrouch : boolean = true;
var canJump : boolean = true;
var raycastOrigin : Transform;
var layerMask : LayerMask;
var rigidVar : Rigidbody;
var playerAnimator : Animator;
var headAnimator : Animator;
var armsAnimator : Animator;
var jumpForce : float;
var direction : Vector3 = Vector3.zero;
private var hit : RaycastHit;
var dist : float = 1;
private var dir : Vector3;
function Update () {
var animationDirection : Vector3;
animationDirection = Vector3(Input.GetAxis("Left/Right"), rigidVar.velocity.y, Input.GetAxis("Backward/Forward"));
direction = Vector3(Input.GetAxis("Left/Right") * realSpeed, rigidVar.velocity.y, Input.GetAxis("Backward/Forward") * realSpeed);
playerAnimator.SetFloat("HorizontalAxis", animationDirection.x);
playerAnimator.SetFloat("VerticalAxis", animationDirection.z);
playerAnimator.SetFloat("ActualYVelocity", animationDirection.y);
direction = transform.TransformDirection(direction);
dir = Vector3(0,-1,0);
Debug.DrawRay(raycastOrigin.position,dir*dist,Color.green);
if(Physics.SphereCast(raycastOrigin.position,0.3,dir,hit,dist,layerMask)){
grounded = true;
}else{
grounded = false;
}
if(Input.GetButton("Fire1")){
armsAnimator.SetBool("Shooting", true);
}else{
armsAnimator.SetBool("Shooting", false);
}
if(Input.GetAxis("Run") > 0.1 && !isCrouch && canRun){
realSpeed = speed * 1.5;
}else if(isCrouch){
realSpeed = speed / 1.2;
}else{
realSpeed = speed;
}
if(Input.GetAxis("Run") > 0.1){
playerAnimator.SetBool("RunButton", true);
headAnimator.SetBool("RunButton", true);
}else{
playerAnimator.SetBool("RunButton", false);
headAnimator.SetBool("RunButton", false);
}
if(direction.x != 0 && direction.z != 0){
playerAnimator.SetBool("WalkButton", true);
headAnimator.SetBool("WalkButton", true);
}else{
playerAnimator.SetBool("WalkButton", false);
headAnimator.SetBool("WalkButton", false);
}
if(isCrouch){
playerAnimator.SetBool("IsCrouching", true);
headAnimator.SetBool("IsCrouching", true);
}else{
playerAnimator.SetBool("IsCrouching", false);
headAnimator.SetBool("IsCrouching", false);
}
if(grounded){
playerAnimator.SetBool("IsOnTheGround", true);
headAnimator.SetBool("IsOnTheGround", true);
}else{
playerAnimator.SetBool("IsOnTheGround", false);
headAnimator.SetBool("IsOnTheGround", false);
isCrouch = false;
}
if(Input.GetButtonDown("Jump")){
if(grounded && canJump && !isCrouch){
rigidVar.AddForce(0,jumpForce * 100,0, ForceMode.Impulse);
playerAnimator.SetBool("JumpButton", true);
}else{
playerAnimator.SetBool("JumpButton", false);
}
}else{
playerAnimator.SetBool("JumpButton", false);
}
if(Input.GetButtonDown("Crouch") && canCrouch && grounded){
isCrouch = !isCrouch;
}
}
function FixedUpdate(){
var realDirection : Vector3 = Vector3(direction.x * realSpeed, 0, direction.z * realSpeed);
rigidVar.MovePosition(transform.position + realDirection * Time.deltaTime);
}