- Home /
NavMesh Agent rotates on X axis when moving and snaps back to 0.
I've been successful in creating a NavMesh at the run time and I am able to get my player unit to move around on the NavMesh. All is well there.
The weird part is that the player unity rotates every so slightly on the X axis but upon reaching the intended destination snaps back to 0 rotation again. My movement is a pretty basic click-to-move system that doesn't involve any rotation other than using the transform.LookAt before moving.
I haven't been able to figure out what is causing this weird rotation or how to stop it from happening yet. Any ideas on what might be causing it would be greatly appreciated.
Answer by unidad2pete · Aug 09, 2017 at 06:14 PM
Please, share your code to find the problem, and tell us if you are using animator.
Answer by behemothdan · Aug 09, 2017 at 06:49 PM
I am not using animator. My player movement code looks like this:
public class PlayerMovement : MonoBehaviour {
public float speed = 3;
private Vector3 targetPosition;
private bool isMoving;
const int LEFT_MOUSE_BUTTON = 0;
private float yAxis;
private float distance;
void Start() {
targetPosition = transform.position;
isMoving = false;
yAxis = gameObject.transform.position.y;
}
void Update () {
if(Input.GetMouseButtonDown(LEFT_MOUSE_BUTTON)) {
SetTargetPosition();
}
if(isMoving) {
MovePlayer();
}
}
void MovePlayer() {
gameObject.transform.LookAt(targetPosition);
distance = Vector3.Distance(targetPosition, gameObject.transform.position);
if(distance >= .5){
gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, targetPosition, speed * Time.deltaTime);
}
if(distance < .5) {
isMoving = false;
}
Debug.DrawLine(transform.position, targetPosition, Color.red);
}
void SetTargetPosition() {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit, 100)) {
targetPosition = hit.point;
targetPosition.y = yAxis;
isMoving = true;
}
}
}
The player object does move properly to the clicked location within the navmesh too. Just the weird X rotation happening is the weird part.
The code its fine. But, you dont need use a nav mesh component , if your player object has a Nav$$anonymous$$esh Agent, remove it, you dont need it, the problem its solved on my test proyect if I dont use Nav$$anonymous$$eshAgent
Ultimately the reason I was using Nav$$anonymous$$eshComponent and Agent was that my map is a non-square that is dynamically generated. Using the Nav$$anonymous$$esh pieces was the best way to keep the player object from moving through walls.
Then,if you are using a Nav$$anonymous$$eshAgent, move your player with them, you dont need use lookAt() , Nav$$anonymous$$eshAgent Looks to de destination automaticaly.
using UnityEngine.AI;
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour
{
public float speed = 3;
private Vector3 targetPosition;
private bool is$$anonymous$$oving;
const int LEFT_$$anonymous$$OUSE_BUTTON = 0;
private float yAxis;
private float distance;
public Nav$$anonymous$$eshAgent agent;
void Start()
{
targetPosition = transform.position;
is$$anonymous$$oving = false;
yAxis = gameObject.transform.position.y;
agent = GetComponent<Nav$$anonymous$$eshAgent>();
}
void Update()
{
if (Input.Get$$anonymous$$ouseButtonDown(LEFT_$$anonymous$$OUSE_BUTTON))
{
SetTargetPosition();
}
if (is$$anonymous$$oving)
{
$$anonymous$$ovePlayer();
}
}
void $$anonymous$$ovePlayer()
{
//gameObject.transform.LookAt(targetPosition);
distance = Vector3.Distance(targetPosition, gameObject.transform.position);
if (distance >= .5)
{
//gameObject.transform.position = Vector3.$$anonymous$$oveTowards(gameObject.transform.position, targetPosition, speed * Time.deltaTime);
agent.SetDestination(targetPosition);
}
if (distance < .5)
{
is$$anonymous$$oving = false;
}
Debug.DrawLine(transform.position, targetPosition, Color.red);
}
void SetTargetPosition()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 100))
{
targetPosition = hit.point;
targetPosition.y = yAxis;
is$$anonymous$$oving = true;
}
}
}
Your answer
Follow this Question
Related Questions
Can't get player to walk to object using NavMeshAgent 1 Answer
Navmesh Agent Path Not Updating 1 Answer
Change agent type stops movement. 0 Answers
Navmesh Agent is moving alone if changed position 1 Answer
Gaps in the Nav mesh 0 Answers