im not trying to copy and paste my scripts to work i actually want to learn this language so please don’t laugh at how awful i is but please explain exactly what is wrong. i am trying to make an animation play when i start moving to my navmesh destination and stop when i reach it.
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
NavMeshAgent navAgent;
// Use this for initialization
void Start ()
{
navAgent = GetComponent<NavMeshAgent> ();
}
// Update is called once per frame
void Update ()
{
RaycastHit Hit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Input.GetMouseButtonDown (0))
{
if (Physics.Raycast (ray, out Hit, 100))
{
navAgent.SetDestination (Hit.point);
if (navAgent.hasPath) {
GetComponent<Animation> ().Play ("walk");
}
if(navAgent.transform.position.Equals (Hit))
GetComponent<Animation>().Play("idle");
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.AI;
public class Movement : MonoBehaviour
{
public float destinationThreshold = 1;
private NavMeshAgent _navAgent;
private RaycastHit _hit; // moved this here so that you can always access it
void Start ()
{
_navAgent = GetComponent<NavMeshAgent> ();
}
void Update ()
{
if (Input.GetMouseButtonDown (0))
{
// moved this here so that you don't create rays for nothing when the mouse button is up
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out _hit, 100))
{
_navAgent.SetDestination (_hit.point);
if (_navAgent.hasPath)
GetComponent<Animation> ().Play ("walk");
}
}
// you will never be exactly at your destination, you can check the distance
// or query _navAgent.remainingDistance
if (Vector3.Distance(_navAgent.transform.position, _hit.point) <= destinationThreshold)
GetComponent<Animation>().Play("idle");
}
}
I am only answering because no one else has…I am not good at C# myself…I use Boo and JS…I see a couple things wrong though you are not telling us what exactly is NOT working as intended…but on that note…You have two if statements…I think it should be an If and an Else If…Otherwise the logic is circular and will be an infinite loop and programs dont like that. Also anytime you are using PHYSICS anything, Collisions, movement, raycast etc…you should use FixedUpdate not Update…Other than that I dont know… I am not great at C# myself…It would be helpful if you told us what is not working.