Question by
ARTOMIA · Jun 10, 2017 at 08:54 AM ·
c#scripting beginnernavmeshnavmeshagenta.i
how can I implement navmesh in this script?
hi, I recently made this script with some help from a tutorial, but i would want my object to use navmesh agent instead of a simple transform, but i don't know a lot about navmesh... still learning
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class WaypointPatrol : MonoBehaviour {
public Transform[] waypoint;
public float patrolSpeed = 3.0f;
public float dampingLook = 6.0f;
public float pauseDuration;
float curTime;
int currentWaypoint;
CharacterController character;
void Start()
{
character = GetComponent<CharacterController>();
}
void Update()
{
if (currentWaypoint < waypoint.Length)
Patrol();
else
currentWaypoint = 0;
}
void Patrol()
{
Vector3 target = waypoint[currentWaypoint].position;
target.y = transform.position.y;
Vector3 moveDirection = target - transform.position;
if (moveDirection.magnitude < 0.5)
{
if (curTime == 0)
curTime = Time.time;
if ((Time.time - curTime) >= pauseDuration)
{
currentWaypoint = Random.Range(0, waypoint.Length);
curTime = 0;
}
}
else
{
var rotation = Quaternion.LookRotation(target - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Creating prefab through Zenject Factory makes navMeshAgent act strange 0 Answers
NavMeshAgent dont find the right way on runtime build NavMesh 0 Answers
Navmesh Agent - The Y Next Position & Y Velocity does not update if there is no X velocity. 0 Answers
How to change speed for a cloned prefab instead of applying it to all instances of prefab? 1 Answer
ProBuilder-made object not generating Nav Mesh (Unity3D) 1 Answer