- Home /
Implementing nav mesh into point and click movement
So I've got a basic point and click movement script but whenever I've tried to implement using a navmesh it doesn't really work correctly. I've got the nav mesh set up properly, as I've got some basic AI that uses it and follows the player. Any help would be much appreciated.
var clickEffect : GameObject;
private var targetPosition:Vector3;
var speed = 60;
function Update () {
if(Input.GetKeyDown(KeyCode.Mouse0))
{
speed = 5;
smooth=1;
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist = 0.0;
if (playerPlane.Raycast (ray, hitdist)) {
var targetPoint = ray.GetPoint(hitdist);
targetPosition = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = targetRotation;
}
}
var dir:Vector3 = targetPosition - transform.position;
var dist:float = dir.magnitude;
var move:float = speed * Time.deltaTime;
if(dist > move){
transform.position += dir.normalized * move;
Instantiate(clickEffect,targetPosition,Quaternion.identity);
animation.CrossFade ("run");
} else {
transform.position = targetPosition;
animation.CrossFade ("idle");
}
transform.position += (targetPosition - transform.position).normalized * speed * Time.deltaTime;
}
function OnCollisionEnter (hit : Collision) {
if (hit.gameObject.tag == "wall") {
print ("hit");
targetPosition = transform.position;
}
}
Well this is my basic movement script. I'm trying to add so that it follows the navmesh ins$$anonymous$$d of just trying to run straight through objects. I tried adding in some navmesh commands but it didn't work. For example I would have "var navComponent : Nav$$anonymous$$eshAgent;" at the top.
then under function Start I would have:
"navComponent = this.transform.GetComponent(Nav$$anonymous$$eshAgent);"
Just not sure if this next bit is correct and where to put it.
"navComponent.SetDestination(target.position);"
This is my movement (link)
What do you mean by, 'doesn't work correctly'? It appears to be working correctly to me. Perhaps you mean it's not working as desired. How would you desire it work?
Like with the navmesh being used. So that when let's say, there's an object in the way. He runs around it, via the navmesh. Ins$$anonymous$$d of straight into it.
Sorry I didn't make that clear.
It seems like you have colliders that allow you to click the walls. If you want to click a wall, but walk around to the other side, then I'd suggest using colliders on a certain layer, and constrain your raycast to that layer only. This way you can control which surfaces in the world respond to touching.
Your answer
Follow this Question
Related Questions
Gaps in the Nav mesh 0 Answers
Navmesh Agent Path Not Updating 1 Answer
NavMeshPath.corners.length is always 0 1 Answer
Problem with Navigation tab 0 Answers
Can't get player to walk to object using NavMeshAgent 1 Answer