Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Making player face direction it is moving

Discussion in 'Scripting' started by Schribly, Dec 10, 2014.

  1. Schribly

    Schribly

    Joined:
    Mar 26, 2014
    Posts:
    4
    Hey all,
    Wasn't sure if this should go into the animations forum or not...
    Basically my player character controls are working fine, the animations go on at the right times, but
    when I am holding the left arrow key the character still does a forward run animation while moving left.
    If that doesn't make sense (which is possible since my mind was just fried by finals) let me know and I can rephrase.
    I tried to fix this by rotating the armature to the correct angle of running but that only works when I get rid of the animations for some reason.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PokemonControl : MonoBehaviour
    5. {
    6.    
    7.     public static float slowMovementSpeed = 24;
    8.     public static float fastMovementSpeed = slowMovementSpeed + (slowMovementSpeed / 2);
    9.     public static float movementSpeed = slowMovementSpeed;
    10.     public float upDownRange = 40.0f;
    11.     public float mouseSpeed = 2.5f;
    12.     float currForward = 0.0f;
    13.     float currSide = 0.0f;
    14.     float forwardSpeed = 0.0f;
    15.     float sideSpeed = 0.0f;
    16.     float vertRot = 0;
    17.     float vertVelocity = 0;
    18.     float xStart;
    19.     float yStart;
    20.     float zStart;
    21.     private float duration = 0;
    22.  
    23.     Animator anim;
    24.     public Transform armature;
    25.     bool runAnim;
    26.     bool runBackAnim;
    27.     bool runLeftAnim;
    28.     bool runRightAnim;
    29.     public bool faint;
    30.     public bool smallDam;
    31.     public static bool pause = false;
    32.  
    33.     // Use this for initialization
    34.     void Start ()
    35.     {
    36.         Screen.lockCursor = true;
    37.         anim = GetComponent<Animator> ();
    38.        
    39.         xStart = transform.position.x;
    40.         yStart = transform.position.y;
    41.         zStart = transform.position.z;
    42.        
    43.         faint = false;
    44.         smallDam = false;
    45.     }
    46.    
    47.     // Update is called once per frame
    48.     void Update ()
    49.     {
    50.         armature.localEulerAngles = new Vector3(-90, 0, -90);
    51.         if (!faint) {
    52.             if(!smallDam) {
    53.                 // Rotation
    54.                
    55.                 if (Input.GetKey("escape") || Input.GetKey("p")) {
    56.                     pause = true;
    57.                 }
    58.                 if (pause) {
    59.                     Time.timeScale = 0;
    60.                 } else {
    61.                     Time.timeScale = 1;
    62.                 }
    63.                 if (!pause) {
    64.                     float rotLR = Input.GetAxis ("Mouse X") * mouseSpeed;
    65.                     transform.Rotate (0, rotLR, 0);
    66.                    
    67.                     vertRot -= Input.GetAxis ("Mouse Y") * mouseSpeed * 0.7f;
    68.                     vertRot = Mathf.Clamp (vertRot, -upDownRange, upDownRange);
    69.                     Camera.main.transform.localRotation = Quaternion.Euler (vertRot, 0, 0);
    70.                 }
    71.                 // Movement
    72.                
    73.                 CharacterController cc = new CharacterController ();
    74.                 cc = GetComponent <CharacterController>();
    75.                
    76.                 currForward = Input.GetAxis ("Vertical");
    77.                 currSide = Input.GetAxis ("Horizontal");
    78.                
    79.                 if (Input.GetKey("left shift")) {
    80.                     movementSpeed = fastMovementSpeed;
    81.                     anim.SetBool ("RUNFAST", true);
    82.                 } else {
    83.                     movementSpeed = slowMovementSpeed;
    84.                     anim.SetBool ("RUN", true);
    85.                     anim.SetBool ("RUNFAST", false);
    86.                 }
    87. //                if(anim.GetBool("RUN"))
    88. //                {
    89.                     forwardSpeed = currForward * movementSpeed * Time.deltaTime;
    90.                     sideSpeed = currSide * movementSpeed * Time.deltaTime;
    91. //                } else {
    92. //                    forwardSpeed = 0;
    93. //                    sideSpeed = 0;
    94. //                }
    95.                
    96.                 vertVelocity += Physics.gravity.y * Time.deltaTime;
    97.                
    98.                 Vector3 speed = new Vector3 (sideSpeed, vertVelocity, forwardSpeed);
    99.                 speed = transform.rotation * speed;
    100.                 if (!anim.GetBool ("ATTACK1") && !anim.GetBool ("ATTACK2") && !anim.GetBool ("ATTACK3") && !anim.GetBool ("ATTACK4")) {
    101.                     cc.Move(speed);
    102.                 }
    103.  
    104.  
    105.                 // Run animation, which way the player should be facing
    106.                 if (forwardSpeed != 0 || sideSpeed != 0) {
    107.                    
    108.                     //forward
    109.                     if ((forwardSpeed > 0) && (sideSpeed == 0) /*&& !anim.GetBool ("ATTACK1") && !anim.GetBool ("ATTACK2")
    110.                         && !anim.GetBool ("ATTACK3") && !anim.GetBool ("ATTACK4")*/) {
    111.                         armature.localEulerAngles = new Vector3(-90, 0, -90);
    112.                        
    113.                         //forward/right
    114.                     } else if ((forwardSpeed > 0)  && (sideSpeed > 0) /*&& !anim.GetBool ("ATTACK1") && !anim.GetBool ("ATTACK2")
    115.                                && !anim.GetBool ("ATTACK3") && !anim.GetBool ("ATTACK4")*/) {
    116.                         armature.localEulerAngles = new Vector3(-90, 45, -90);
    117.                        
    118.                         //forward/left
    119.                     } else if ((forwardSpeed > 0)  && (sideSpeed < 0) /*&& !anim.GetBool ("ATTACK1") && !anim.GetBool ("ATTACK2")
    120.                                && !anim.GetBool ("ATTACK3") && !anim.GetBool ("ATTACK4")*/) {
    121.                         armature.localEulerAngles = new Vector3(-90, -45, -90);
    122.                        
    123.                         //backward
    124.                     } else if ((forwardSpeed < 0)  && (sideSpeed == 0) /*&& !anim.GetBool ("ATTACK1") && !anim.GetBool ("ATTACK2")
    125.                                && !anim.GetBool ("ATTACK3") && !anim.GetBool ("ATTACK4")*/) {
    126.                         armature.localEulerAngles = new Vector3(-90, 180, -90);
    127.                        
    128.                         //backward/right
    129.                     } else if ((forwardSpeed < 0)  && (sideSpeed > 0) /*&& !anim.GetBool ("ATTACK1") && !anim.GetBool ("ATTACK2")
    130.                                && !anim.GetBool ("ATTACK3") && !anim.GetBool ("ATTACK4")*/) {
    131.                         armature.localEulerAngles = new Vector3(-90, 135, -90);
    132.                        
    133.                         //backward/left
    134.                     } else if ((forwardSpeed < 0)  && (sideSpeed < 0) /*&& !anim.GetBool ("ATTACK1") && !anim.GetBool ("ATTACK2")
    135.                                && !anim.GetBool ("ATTACK3") && !anim.GetBool ("ATTACK4")*/) {
    136.                         armature.localEulerAngles = new Vector3(-90, 225, -90);
    137.                        
    138.                         //right
    139.                     } else if((forwardSpeed == 0)  && (sideSpeed > 0) /*&& !anim.GetBool ("ATTACK1") && !anim.GetBool ("ATTACK2")
    140.                               && !anim.GetBool ("ATTACK3") && !anim.GetBool ("ATTACK4")*/){
    141.                         armature.localEulerAngles = new Vector3(-90, 90, -90);
    142.                        
    143.                         //left
    144.                     } else if((forwardSpeed == 0)  && (sideSpeed < 0) /*&& !anim.GetBool ("ATTACK1") && !anim.GetBool ("ATTACK2")
    145.                               && !anim.GetBool ("ATTACK3") && !anim.GetBool ("ATTACK4")*/){
    146.                         armature.localEulerAngles = new Vector3(-90, -90, -90);
    147.                     }
    148.                 } else {
    149.                     anim.SetBool ("RUN", false);
    150.                     anim.SetBool ("RUNFAST", false);
    151.                 }
    152.             }
    153.         }
    154.     }
    155. }
    156.  
     

    Attached Files:

  2. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    You could move it via rigidbody and lerp it's transform.forward vector to its velocity vector.