- Home /
Question by
brawler93 · Mar 28, 2016 at 12:27 AM ·
movementnavmeshagentpathfinding
pathfinding with instant movement(no turning)
hi,
I'm trying to make a movement/pathfinding script where the character can (almost) instantly turn 180 degrees. Can this be made possible with navmeshagent. Im new to unity so any help would be highly appriciated.
using UnityEngine;
using System.Collections;
[DisallowMultipleComponent]
[RequireComponent(typeof(NavMeshAgent))]
public class PlayerMovementNavMesh : MonoBehaviour {
private Vector3 targetPosition;
const int LEFT_MOUSE_BUTTON = 0;
NavMeshAgent agent;
/// Cache all of our components that need it.
void Awake()
{
agent = GetComponent<NavMeshAgent>();
}
/// Use this for initialization.
void Start ()
{
targetPosition = transform.position;
}
void Update ()
{
//if the player clicked on the screen, find out where
if(Input.GetMouseButton(LEFT_MOUSE_BUTTON))
SetTargetPosition();
MovePlayer();
}
void SetTargetPosition()
{
Plane plane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float point = 0f;
if(plane.Raycast (ray, out point))
targetPosition = ray.GetPoint(point);
}
void MovePlayer()
{
agent.angularSpeed = 360;
agent.acceleration = 999999999;
agent.SetDestination(targetPosition);
}
}
Comment