Hi there, in a top down click-to-move scenario, I’d like to rotate my Navmesh agent and only after it’s rotated towards the target destination should the Navmesh agent move forward (think dota movement).
I’m getting confused on how to do this… Here’s my code so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class ClickToMove : MonoBehaviour
{
public static ClickToMove instance;
public Camera cam;
public NavMeshAgent agent;
private Vector3 targetPosition;
private Vector3 lookAtTarget;
private Quaternion playerRot;
private float rotSpeed = 55;
void Update()
{
cam = Camera.main;
if (Input.GetMouseButtonDown(1))
{
SetTargetPosition();
}
Move();
}
void SetTargetPosition()
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1000))
{
targetPosition = hit.point;
lookAtTarget = new Vector3(targetPosition.x - transform.position.x, transform.position.y, targetPosition.z - transform.position.z);
playerRot = Quaternion.LookRotation(lookAtTarget);
}
}
void Move()
{
this.transform.rotation = Quaternion.Slerp(transform.rotation, playerRot, rotSpeed * Time.deltaTime);
agent.SetDestination(targetPosition);
}
}
(Any help would be GREATLY appreciated)