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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help with player switching between attacks, idle, and walk? (noob)

Discussion in 'Scripting' started by Sunniiee, Oct 11, 2015.

  1. Sunniiee

    Sunniiee

    Joined:
    Aug 29, 2015
    Posts:
    6
    Hi I'm having problem with my animation. Switching between Idle and Walk animation is okay but whenever I try to switch it to Idle to Attack or Attack to Idle or Walk to Attack or Attack to Walk it doesn't seem to work.

    Help?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlScript: MonoBehaviour {
    5.    
    6.     public float lookSpeed = 10;
    7.     Vector3 curLoc;
    8.     Vector3 prevLoc;
    9.  
    10.  
    11.     float idlechecker;
    12.     bool walk, idle, guard, kote, doo, men, tsuki;
    13.     Animator PlayerAni;
    14.     Vector3 currentrot;
    15.     int Anitimer = 0 ;
    16.  
    17.     float speed = 0.5f,TurnSpeed = 2;
    18.     void Start () {
    19.         PlayerAni = GetComponent<Animator>();
    20.         walk = false;
    21.         idle = false;
    22.         guard = false;
    23.         kote = false;
    24.         doo = false;
    25.         men = false;
    26.         tsuki = false;
    27.  
    28.     }
    29.  
    30.     void Update ()
    31.     {
    32.         currentrot = transform.localEulerAngles;
    33.  
    34.         ///animation
    35.         PlayerAni.SetBool ("Move", walk);
    36.         PlayerAni.SetBool ("Idle", idle);
    37.         PlayerAni.SetBool ("Guard", guard);
    38.         PlayerAni.SetBool ("Kote", kote); //attack1
    39.         PlayerAni.SetBool ("Do", doo); //attack2
    40.         PlayerAni.SetBool ("Men", men); //attack3
    41.         PlayerAni.SetBool ("Tsuki", tsuki);//attack4
    42.  
    43.         idlechecker = (Input.GetAxis ("Horizontal") + Input.GetAxis ("Vertical"));
    44.         if(idlechecker == 0)
    45.         {      
    46.             isIdle ();
    47.  
    48.         }
    49.         else if (idlechecker != 0) {
    50.             isWalking ();
    51.         }
    52.  
    53.         if (Input.GetKey (KeyCode.H)) {
    54.             SkillDo ();
    55.         }
    56.         if (Input.GetKey (KeyCode.J)) {
    57.             SkillKote();
    58.         }
    59.         if (Input.GetKey (KeyCode.K)) {
    60.             SkillMen();
    61.         }
    62.         if (Input.GetKey (KeyCode.L)) {
    63.             SkillTsuki();
    64.         }
    65.         if (Input.GetKey (KeyCode.U)) {
    66.             isGuarding();
    67.         }
    68.  
    69.     }
    70.    
    71.     private void InputListen()
    72.     {
    73.         prevLoc = curLoc;
    74.         curLoc = transform.position;
    75.        
    76.         if(Input.GetKey(KeyCode.A))
    77.             curLoc.x -= 1 * Time.fixedDeltaTime;
    78.         if(Input.GetKey(KeyCode.D))
    79.             curLoc.x += 1 * Time.fixedDeltaTime;
    80.         if(Input.GetKey(KeyCode.W))
    81.             curLoc.z += 1 * Time.fixedDeltaTime;
    82.         if(Input.GetKey(KeyCode.S))
    83.             curLoc.z -= 1 * Time.fixedDeltaTime;
    84.        
    85.         transform.position = curLoc;
    86.        
    87.     }
    88.  
    89.     //movements
    90.     void isIdle()
    91.     {
    92.         walk = false;
    93.         idle = true;
    94.         guard = false;
    95.         kote = false;
    96.         doo = false;
    97.         men = false;
    98.         tsuki = false;
    99.  
    100.     }
    101.     void isWalking()
    102.     {
    103.         InputListen();
    104.         transform.rotation = Quaternion.Lerp
    105.             (transform.rotation,  Quaternion.LookRotation(transform.position - prevLoc), Time.fixedDeltaTime * lookSpeed);
    106.         walk = true;
    107.         idle = false;
    108.         guard = false;
    109.         kote = false;
    110.         doo = false;
    111.         men = false;
    112.         tsuki = false;
    113.  
    114.     }
    115.     void isGuarding()
    116.     {
    117.         walk = false;
    118.         idle = false;
    119.         guard = true;
    120.         kote = false;
    121.         doo = false;
    122.         men = false;
    123.         tsuki = false;
    124.        
    125.     }
    126.     void SkillKote()
    127.     {
    128.         walk = false;
    129.         idle = false;
    130.         guard = false;
    131.         kote = true;
    132.         doo = false;
    133.         men = false;
    134.         tsuki = false;
    135.  
    136.     }
    137.     void SkillDo()
    138.     {
    139.         walk = false;
    140.         idle = false;
    141.         guard = false;
    142.         kote = false;
    143.         doo = true;
    144.         men = false;
    145.         tsuki = false;
    146.  
    147.  
    148.  
    149.     }
    150.     void SkillMen()
    151.     {
    152.         walk = false;
    153.         idle = false;
    154.         guard = false;
    155.         kote = false;
    156.         doo = false;
    157.         men = true;
    158.         tsuki = false;
    159.  
    160.  
    161.     }
    162.     void SkillTsuki()
    163.     {
    164.         walk = false;
    165.         idle = false;
    166.         guard = false;
    167.         kote = false;
    168.         doo = false;
    169.         men = false;
    170.         tsuki = true;
    171.  
    172.  
    173.     }
    174.  
    175.  
    176. }
     
  2. Sunniiee

    Sunniiee

    Joined:
    Aug 29, 2015
    Posts:
    6
    nevermind i found an answer already lol