Search Unity

How can i store speed and direction as a float for mecanim?

Discussion in 'Scripting' started by JohnPet, Dec 24, 2013.

  1. JohnPet

    JohnPet

    Joined:
    Aug 19, 2012
    Posts:
    85
    Here i have a directional controller and i am trying to store two variables that calculate the movement direction and speed. Direction: a float to use for the direction float in mecanim is used. This is for using the tilting run animations when the character changes direction, and continues to normal run animation.
    Speed: This is to calculate the movement speed. Currently, the Mathf.Clamp works good, but the character walks non stop after input.

    Only moveDirection variable is Vector3.zero.

    Here is the code bit:
    Code (csharp):
    1.  
    2. void Update()
    3.     {
    4.         CharacterController controller = GetComponent<CharacterController>();
    5.        
    6.         float h = Input.GetAxis("Horizontal");
    7.         float v = Input.GetAxis("Vertical");
    8.        
    9.         moveDirection = new Vector3(h, 0, v);
    10.         speed = Mathf.Clamp(direction.magnitude, 0, 1);
    11.        
    12.         if(moveDirection != Vector3.zero)
    13.         {
    14.             moveDirection = mainCam.transform.TransformDirection(moveDirection);
    15.             moveDirection.y = 0;
    16.            
    17.             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(moveDirection),  directionSpeed * Time.deltaTime);
    18.            
    19.             moveDirection = transform.forward;
    20.            
    21.             direction = mainCam.transform.TransformDirection(direction);
    22.             direction = new Vector3(h, 0, v);
    23.            
    24.            
    25.             mec.SetFloat("Speed", speed);
    26.             mec.SetFloat("Direction", speed);
    27.         }
    28.  
    Thank you for reading.
     
  2. JohnPet

    JohnPet

    Joined:
    Aug 19, 2012
    Posts:
    85
    Bump..