Question by
chas02068909 · Mar 31, 2016 at 04:25 PM ·
buttonnavmeshnavmeshagentanimator controlleronclick
Move character in a destination with OnClick button
Hello ; I Try to move my character in a destination, the movement begin when I click on the a button. The problem is that I have to click two time on the button to activate the animator walk animation. The fist click move the character but without animation. Can you Help me please with the script. Thanks using UnityEngine; using System.Collections;
public class NPCMovement : MonoBehaviour {
NavMeshAgent agent;
Animator anim;
public Transform destination;
[Range(0.2f, 2.5f)]
public float speed = 1.0f;
// Use this for initialization
void Start () {
agent = GetComponent<NavMeshAgent>();
}
public void GoTo () {
agent.destination = destination.position;
if (anim != null || (anim = GetComponent<Animator>()))
{
anim.SetFloat ("Speed", speed);
anim.speed = speed;
agent.velocity = anim.velocity;
if (agent.desiredVelocity.magnitude > 0.1f)
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(agent.desiredVelocity), 0.1f);
if (agent.remainingDistance < 1.0f)
{
anim.SetFloat("Speed", 0.0f);
anim.speed = 1.0f;
}
Vector3 agentOffset = agent.nextPosition - transform.position;
if (agentOffset.magnitude > 0.3f)
{
agent.nextPosition = transform.position;
}
}
}
}
Comment