Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Walking and attack animation of character appearing broken

Discussion in 'Animation' started by Gameatro, Mar 31, 2020.

  1. Gameatro

    Gameatro

    Joined:
    Sep 15, 2017
    Posts:
    9
    I am trying to make a small game where a zombie follows the player and attacks on getting near. But it is appearing broken. Here is the video:

    Here is my code controlling the Zombie:
    void Update()
    {
    agent.SetDestination(player.position);
    if (agent.pathPending)
    return;
    float speed = 0.0f;
    if (agent.remainingDistance <= agent.stoppingDistance*stopDistanceProportion )
    {
    agent.isStopped = true;
    animator.SetBool("PlayerClose", true);

    transform.LookAt(player);
    }
    else if(agent.remainingDistance < 50.0f)
    {
    float proportionalDistance = 1f - agent.remainingDistance / agent.stoppingDistance;
    transform.LookAt(player);

    speed = Mathf.Lerp(0.5f, 1f, proportionalDistance);
    agent.isStopped = false;
    animator.SetBool("PlayerClose", false);
    }
    else if (agent.remainingDistance < 150.0f)
    {
    Quaternion targetRotation = Quaternion.LookRotation(agent.desiredVelocity);
    float proportionalDistance = 1f - agent.remainingDistance / agent.stoppingDistance;
    transform.LookAt(player);
    speed = Mathf.Lerp(0f, 0.5f, proportionalDistance);
    agent.isStopped = false;
    animator.SetBool("PlayerClose", false);
    }
    else
    {
    agent.isStopped = true;
    animator.SetBool("PlayerClose", false);
    speed = 0f;
    }
    animator.SetFloat("Speed", speed);

    }
    The zombie starts attacking animation when "PlayerClose" is set true and the Speed is used to control walking, running and Idle. Idle on 0, walk on 0-0.5 and run on 0.5 to 1. So why is this happening? I am new to Unity animations, so please guide me.
     
  2. DARTHMAYDAR11

    DARTHMAYDAR11

    Joined:
    Sep 30, 2015
    Posts:
    8
    Hey look into your animator controller. Open it up and look at the transitions you can change the amount of time it takes to blend between two animations, and you can also adjust the exit time. It may just be the animation is exiting early or the transitions is too long between animations. This can be changed by clicking on the transition lines in the animator window.