Search Unity

i need another way to use the old animation.Play

Discussion in 'Scripting' started by sukottosama, May 4, 2015.

  1. sukottosama

    sukottosama

    Joined:
    Apr 30, 2015
    Posts:
    4
    i 100% sure animation.play is an older way of playing animations. i've been using GetComponent<Animation> ().play but i've run into problems later on in my code.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Fighter : MonoBehaviour {
    5.     public GameObject opponent;
    6.  
    7.     public AnimationClip attack;
    8.     public int damage;
    9.     public double impactTime;
    10.     private double impactLength;
    11.     public bool impacted;
    12.     public float range;
    13.  
    14.     // Use this for initialization
    15.     void Start ()
    16.     {
    17.         impactLength = (GetComponent<Animation>()[attack.name].length*impactTime);
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update ()
    22.     {
    23.         if(Input.GetKey(KeyCode.Space)&&inRange())
    24.         {
    25.             GetComponent<Animation>().Play(attack.name);
    26.             ClickToMove.attack = true;
    27.             if(opponent != null)
    28.             {
    29.                 transform.LookAt(opponent.transform.position);
    30.                 opponent.GetComponent<Mob>().getHit(damage);
    31.             }
    32.         }
    33.         if(GetComponent<Animation>()[attack.name].time>0.9*GetComponent<Animation>()[attack.name].length)
    34.         {
    35.             ClickToMove.attack = false;
    36.             impacted = false;
    37.         }
    38.         impact ();
    39.     }
    40.     void impact()
    41.     {
    42.         if(opponent != null&&GetComponent<Animation>().IsPlaying(attack.name)&&!impacted)
    43.         {
    44.             if(GetComponent<Animation>()[attack.name].time>impactLength
    45.                &&(GetComponent<Animation>()[attack.name].time<0.9*GetComponent<Animation>()[attack.name].length))
    46.             {
    47.                 opponent.GetComponent<Mob>().getHit(damage);
    48.                 impacted = true;
    49.             }
    50.         }
    51.     }
    52.     bool inRange(){
    53.         if (Vector3.Distance (opponent.transform.position, transform.position) <= range) {
    54.             return true;
    55.         } else {
    56.             return false;
    57.         }
    58.     }
    59. }
    i am unfamiliar with unity api and slowly learning; any tips or advice will be great
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    What problems are you running into? We need more info about what is happening.
     
  3. sukottosama

    sukottosama

    Joined:
    Apr 30, 2015
    Posts:
    4
    basely i trying to have it where only damage when the animation hit's but i can see in console it hits multiple times in one animation. i didn't want to post all of the code because it looks like spam but yolo sorry for grammar still half sleep. >.>
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ClickToMove : MonoBehaviour {
    5.  
    6.     public float speed;
    7.     public CharacterController controller;
    8.     private Vector3 position;
    9.     public static bool attack;
    10.  
    11.     public AnimationClip run;
    12.     public AnimationClip idle;
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.         position = transform.position;
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.         if (!attack) {
    22.             if (Input.GetMouseButton (0)) {
    23.                 locatePosition ();
    24.             }
    25.             moveToPosition ();
    26.         } else {
    27.            
    28.         }
    29.     }
    30.     void locatePosition(){
    31.         RaycastHit hit;
    32.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    33.  
    34.         if(Physics.Raycast(ray, out hit, 1000)){
    35.             if(hit.collider.tag != "Player"&&hit.collider.tag!="Enemy"){
    36.                 position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
    37.             }
    38.         }
    39.     }
    40.     void moveToPosition(){
    41.         if (Vector3.Distance (transform.position, position) > 1) {
    42.             Quaternion newRotation = Quaternion.LookRotation (position - transform.position, Vector3.forward);
    43.  
    44.             newRotation.x = 0f;
    45.             newRotation.z = 0f;
    46.  
    47.             transform.rotation = Quaternion.Slerp (transform.rotation, newRotation, Time.deltaTime + 10);
    48.             controller.SimpleMove (transform.forward * speed);
    49.             GetComponent<Animation>().CrossFade(run.name);
    50.         } else {
    51.             GetComponent<Animation>().CrossFade(idle.name);
    52.         }
    53.     }
    54. }
    55.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Mob : MonoBehaviour {
    5.     public float speed;
    6.     public float range;
    7.  
    8.     public Transform player;
    9.     public CharacterController controller;
    10.  
    11.     public AnimationClip run;
    12.     public AnimationClip idle;
    13.     public AnimationClip die;
    14.  
    15.     private int Health;
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.         Health = 100;
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void Update () {
    24.         if (!isDead ()) {
    25.             if (!inRange ()) {
    26.                 chase ();
    27.             } else {
    28.                 GetComponent<Animation> ().CrossFade (idle.name);
    29.             }
    30.         } else {
    31.             dieMethod();
    32.         }
    33.     }
    34.     public void getHit(int damage){
    35.         Health = Health - damage;
    36.         Debug.Log (Health);
    37.         if(Health < 0){
    38.             Health = 0;
    39.         }
    40.     }
    41.  
    42.     bool inRange(){
    43.         if (Vector3.Distance (transform.position, player.position) < range) {
    44.             return true;
    45.         } else {
    46.             return false;
    47.         }
    48.     }
    49.     void chase(){
    50.         transform.LookAt (player.position);
    51.         controller.SimpleMove (transform.forward* speed);
    52.         GetComponent<Animation> ().CrossFade (run.name);
    53.     }
    54.     void dieMethod(){
    55.         GetComponent<Animation> ().Play (die.name);
    56.         if(GetComponent<Animation>()[die.name].time>GetComponent<Animation>()[die.name].length*0.9){
    57.             Destroy(gameObject);
    58.         }
    59.     }
    60.     bool isDead() {
    61.         if (Health <= 0) {
    62.             return true;
    63.         } else {
    64.             return false;
    65.         }
    66.     }
    67.     void OnMouseOver(){
    68.         player.GetComponent<Fighter> ().opponent = gameObject;
    69.     }
    70. }
     
  4. sukottosama

    sukottosama

    Joined:
    Apr 30, 2015
    Posts:
    4
    i hope this helps sorry kinda just getting into c# and programming
    Code (CSharp):
    1. //example
    2. public AnimationClip die;
    3. void Update()
    4. {
    5.      //this is the old function that my friend is telling me i need.
    6.      animation.Play(die.name);
    7.      //this is the function the online api is telling me to use
    8.     /* yet, i get an error
    9.      * "Assets/Mob.cs(55,27): error CS0120:
    10.      * An object reference is required to access non-static
    11.      * member `UnityEngine.Animation.Play()'
    12.      */
    13.     Animation.Play(die.name);
    14.      //and this is the only way that works
    15.     GetComponent<Animation>().Play(die.name);
    16. }
     
    Last edited: May 5, 2015
  5. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    Whats happening is, your update is seeing if the key is down and calling all the functions for attacking. It will continue to call all those functions as long as the key is down.

    If you switch out GetKey with GetKeyDown it should work.

    GetKey gets the current state of the key, true if down and false if up.

    GetKeyDown only returns true on the first frame the key is pressed, and false every other frame until you let the key go and press it again.

    GetKeyUp only returns true on the first frame the key is let go, then will return false the rest of the time until the key is pressed, then let go again.

    There is probably more issues with some of the logic in your code, but you need to work off of that for now.
     
    sukottosama likes this.
  6. sukottosama

    sukottosama

    Joined:
    Apr 30, 2015
    Posts:
    4
    oh okay, that works, i didn't think of look at input. i thought it was a problem with the way of i was using Animation. btw unity needs to fix their online api. missing stuff and out of date info... etc thanks for the help
     
  7. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    Agreed!

    Glad to help :)