Search Unity

get the local speed of character controller?

Discussion in 'Scripting' started by killa247, Jul 24, 2014.

  1. killa247

    killa247

    Joined:
    May 7, 2013
    Posts:
    25
    Hey, so i have the player moving and i have some animations, i just need to get the local or relative speeds of the player character controller (using the default fps controller) so when i press "a" the speed is local to the players moving left in any direction he is facing

    heres my code
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class animation : MonoBehaviour {
    5.  
    6.     Animator anim;
    7.     void Start ()
    8.     {
    9.         anim = GetComponent<Animator>();
    10.     }
    11.    
    12.    
    13.     void Update ()
    14.     {
    15.  
    16.         CharacterController controller = GetComponentInParent<CharacterController>();
    17.         Vector3 horizontalVelocity = controller.velocity;
    18.         horizontalVelocity = new Vector3(controller.velocity.x, 0, controller.velocity.z);
    19.         float horizontalSpeed = horizontalVelocity.magnitude;
    20.         float verticalSpeed = controller.velocity.y;
    21.         float overallSpeed = controller.velocity.magnitude;
    22.  
    23.         Vector3 horizontalVelocityX = controller.velocity;
    24.         horizontalVelocityX = new Vector3(controller.velocity.x, 0, 0);
    25.         float horizontalSpeedX = horizontalVelocityX.magnitude;
    26.  
    27.         Vector3 horizontalVelocityZ = controller.velocity;
    28.         horizontalVelocityZ = new Vector3(0, 0, controller.velocity.z);
    29.         float horizontalSpeedZ = horizontalVelocityZ.magnitude;
    30.  
    31.         anim.SetFloat("VelocityX", horizontalSpeedX);
    32.         anim.SetFloat("VelocityZ", horizontalSpeedZ);
    33.         anim.SetFloat("Speed", horizontalSpeed);
    34. }}