i need another way to use the old animation.Play

i 100% sure animation.play is an older way of playing animations. i’ve been using GetComponent ().play but i’ve run into problems later on in my code.

using UnityEngine;
using System.Collections;

public class Fighter : MonoBehaviour {
    public GameObject opponent;

    public AnimationClip attack;
    public int damage;
    public double impactTime;
    private double impactLength;
    public bool impacted;
    public float range;

    // Use this for initialization
    void Start ()
    {
        impactLength = (GetComponent<Animation>()[attack.name].length*impactTime);
    }
   
    // Update is called once per frame
    void Update ()
    {
        if(Input.GetKey(KeyCode.Space)&&inRange())
        {
            GetComponent<Animation>().Play(attack.name);
            ClickToMove.attack = true;
            if(opponent != null)
            {
                transform.LookAt(opponent.transform.position);
                opponent.GetComponent<Mob>().getHit(damage);
            }
        }
        if(GetComponent<Animation>()[attack.name].time>0.9*GetComponent<Animation>()[attack.name].length)
        {
            ClickToMove.attack = false;
            impacted = false;
        }
        impact ();
    }
    void impact()
    {
        if(opponent != null&&GetComponent<Animation>().IsPlaying(attack.name)&&!impacted)
        {
            if(GetComponent<Animation>()[attack.name].time>impactLength
               &&(GetComponent<Animation>()[attack.name].time<0.9*GetComponent<Animation>()[attack.name].length))
            {
                opponent.GetComponent<Mob>().getHit(damage);
                impacted = true;
            }
        }
    }
    bool inRange(){
        if (Vector3.Distance (opponent.transform.position, transform.position) <= range) {
            return true;
        } else {
            return false;
        }
    }
}

i am unfamiliar with unity api and slowly learning; any tips or advice will be great

What problems are you running into? We need more info about what is happening.

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. >.>

using UnityEngine;
using System.Collections;

public class ClickToMove : MonoBehaviour {

    public float speed;
    public CharacterController controller;
    private Vector3 position;
    public static bool attack;

    public AnimationClip run;
    public AnimationClip idle;

    // Use this for initialization
    void Start () {
        position = transform.position;
    }
   
    // Update is called once per frame
    void Update () {
        if (!attack) {
            if (Input.GetMouseButton (0)) {
                locatePosition ();
            }
            moveToPosition ();
        } else {
           
        }
    }
    void locatePosition(){
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);

        if(Physics.Raycast(ray, out hit, 1000)){
            if(hit.collider.tag != "Player"&&hit.collider.tag!="Enemy"){
                position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
            }
        }
    }
    void moveToPosition(){
        if (Vector3.Distance (transform.position, position) > 1) {
            Quaternion newRotation = Quaternion.LookRotation (position - transform.position, Vector3.forward);

            newRotation.x = 0f;
            newRotation.z = 0f;

            transform.rotation = Quaternion.Slerp (transform.rotation, newRotation, Time.deltaTime + 10);
            controller.SimpleMove (transform.forward * speed);
            GetComponent<Animation>().CrossFade(run.name);
        } else {
            GetComponent<Animation>().CrossFade(idle.name);
        }
    }
}
using UnityEngine;
using System.Collections;

public class Mob : MonoBehaviour {
    public float speed;
    public float range;

    public Transform player;
    public CharacterController controller;

    public AnimationClip run;
    public AnimationClip idle;
    public AnimationClip die;

    private int Health;

    // Use this for initialization
    void Start () {
        Health = 100;
    }
   
    // Update is called once per frame
    void Update () {
        if (!isDead ()) {
            if (!inRange ()) {
                chase ();
            } else {
                GetComponent<Animation> ().CrossFade (idle.name);
            }
        } else {
            dieMethod();
        }
    }
    public void getHit(int damage){
        Health = Health - damage;
        Debug.Log (Health);
        if(Health < 0){
            Health = 0;
        }
    }

    bool inRange(){
        if (Vector3.Distance (transform.position, player.position) < range) {
            return true;
        } else {
            return false;
        }
    }
    void chase(){
        transform.LookAt (player.position);
        controller.SimpleMove (transform.forward* speed);
        GetComponent<Animation> ().CrossFade (run.name);
    }
    void dieMethod(){
        GetComponent<Animation> ().Play (die.name);
        if(GetComponent<Animation>()[die.name].time>GetComponent<Animation>()[die.name].length*0.9){
            Destroy(gameObject);
        }
    }
    bool isDead() {
        if (Health <= 0) {
            return true;
        } else {
            return false;
        }
    }
    void OnMouseOver(){
        player.GetComponent<Fighter> ().opponent = gameObject;
    }
}

i hope this helps sorry kinda just getting into c# and programming

//example
public AnimationClip die;
void Update()
{
     //this is the old function that my friend is telling me i need.
     animation.Play(die.name);
     //this is the function the online api is telling me to use
    /* yet, i get an error
     * "Assets/Mob.cs(55,27): error CS0120:
     * An object reference is required to access non-static
     * member `UnityEngine.Animation.Play()'
     */
    Animation.Play(die.name);
     //and this is the only way that works
    GetComponent<Animation>().Play(die.name);
}

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.

1 Like

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

Agreed!

Glad to help :slight_smile: