Hi guys…
I’m having some issues in what referes with my character movement… The question is, im using 1st navemesh to move the character if im in image location and try to go point A everythings goes well if i dont try back before it reachs the A point. If i click to go point A and before it takes it i click to point B instead of just turn and move the player describes that kinda of curve and before it curve it runs a bit forward first and then it goes fine at B point. Wt i would like to know is if this is a scripting problem or since im using a squeleton asset could be the animation that come with problem but i really dont thing thats an animation problem. If someone can help i will appreciate…
I will post the script and the image.
using UnityEngine;
[RequireComponent(typeof(NavMeshAgent))]
public class Move : MonoBehaviour
{
public AnimationClip idle;
public AnimationClip run;
public enum MouseButtonType {Left, Right, Middle};
public MouseButtonType mouseButton = MouseButtonType.Left;
public Transform myTransform;
private NavMeshAgent navMeshAgent;
private Animation anim;
void Awake()
{
myTransform = transform;
anim = GetComponent<Animation> ();
navMeshAgent = GetComponent<NavMeshAgent> ();
}
// Update is called once per frame
void Update ()
{
if (navMeshAgent.remainingDistance < 0.5f)
{
if (idle != null && anim != null) anim.CrossFade (idle.name);
} else
{
if(run != null && anim != null) anim.CrossFade (run.name);
}
//Move player if left mouse button is clicked
if (Input.GetMouseButtonDown ((int)mouseButton) && GUIUtility.hotControl == 0)
{
Plane playerPlane = new Plane (Vector3.up, myTransform.position);
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast (ray, out hitdist))
{
navMeshAgent.SetDestination (ray.GetPoint (hitdist));
}
}
//Move player if the mouse button is held down
else if (Input.GetMouseButton ((int)mouseButton) && GUIUtility.hotControl == 0)
{
Plane playerPlane = new Plane (Vector3.up, myTransform.position);
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float hitdist = 0.0f;
if (playerPlane.Raycast (ray, out hitdist))
{
navMeshAgent.SetDestination (ray.GetPoint (hitdist));
}
}
}
}