Home > Enterprise >  My animation is not returning to idle but goes from idle to run animation and then never returns
My animation is not returning to idle but goes from idle to run animation and then never returns

Time:07-05

I need help my animation is not returning back to idle this is for my enemy. I have the transitions set up in the animator and i have a bool parameter for true and false and no I don't want to use unity built in ai thing.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BossMovement : MonoBehaviour
{
    public Animator anim;
    public float distance;
    public Transform target;
    public float speed = 4f;
    Rigidbody rig;
    // Start is called before the first frame update
    void Start()
    {
        rig = GetComponent<Rigidbody>();
    }

    private void Update()
    {
        whento();
        transform.LookAt(target);
    }
    // Update is called once per frame
    void followPlayer()
    {
        Vector3 pos = Vector3.MoveTowards(transform.position, target.position, speed * Time.fixedDeltaTime);
        rig.MovePosition(pos);
        transform.LookAt(target);
    }
    void whento()
    {
        RaycastHit hit;
        if (Physics.Raycast(transform.position, transform.forward, out hit, distance))
        {

            if (hit.transform.tag == "Player")
            {
                followPlayer();
                anim.SetBool("isMoving", true);
            }

            else if (hit.transform.tag != "Player")
            {
                anim.SetBool("isMoving", false);
            }
        }
        

    }
}

    

CodePudding user response:

Try replacing the last else if Statement to just an else Statement. This way your code works regardless if you hit something or not.

  • Related