Question About NavMesh Agent and rotation
So I have a NavMesh Agent on a Enemy Object. However, I also have a script on this object, here is the script: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; //NEED
public class EnemyPathFinding : MonoBehaviour {
public float lookRadius = 10; //how close till it starts following?
Transform target;
NavMeshAgent agent;
public GameObject CharacterList;
public PlacingCharacters placingCharacters;
// Use this for initialization
void Start()
{
#region Getting ReferencesS
int PlayerIndex = PlayerPrefs.GetInt("Player");
int OpponentIndex = PlayerPrefs.GetInt("Opponent");
GameObject player = placingCharacters.CharacterList[PlayerIndex];
if(gameObject == player.gameObject)
{//if the player and the gameObject are the same
gameObject.GetComponent<NavMeshAgent>().enabled = false;
Destroy(this, 0);
}
#endregion
target = player.transform;
agent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
float distance = Vector3.Distance(transform.position, target.position);
if (distance <= lookRadius)
{//if the player is in the look radius
agent.SetDestination(target.position); //set destination
//update rotation
if (distance <= agent.stoppingDistance)
{//if it is stopped
//attack
//face
FaceTarget();
}
}
}
public void FaceTarget()
{
//get difference of the rotation of the player and gameObjects position
Vector3 direction = (target.position - transform.position).normalized;
//set lookRotation to the x and y of the player
Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
//apply rotation
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5);
}
}
Now, I have a rigid body on this Object because I am wanting to make it affected by objects, for example, if I as a player "push" the object, I want it to fall down. However, when I "push" it down, it does not move. It's as if the rotation is frozen. I did check the Rigid body constraints, and there is no constraints on the object.
Thank you in advance!
Answer by Slink472 · Sep 22, 2017 at 01:47 AM
Hi! Let me know if I'm understanding the problem right, but you just want to be able to knock your enemy around like any other rigidbody? I ran into the same problem, and it turns out that navmesh agents cannot be acted on by normal physics like you want. To have them knocked around, you have to have the enemy disable its navmesh component. You can then reenable it when the enemy lands on the ground again.
I read that it's good practice to have your agents' rigidbody set to kinematic while on the navmesh, changing that value when airborne is probably also a good idea. My answer is by no means definitive, that's just what I've found to work for my project and I'd welcome anyone else's comments on it.
Your answer
Follow this Question
Related Questions
Question about NavMesh and Rigidbody 0 Answers
Torque rigidbody toward desired rotation on two axes ignoring Y axes? 0 Answers
unity 2d rotation flipping on y axis upside-down problems 0 Answers
Problem of stuttering FPS Camera 1 Answer
Issues Getting my GameObject to both Move Forward and Jump properly. 0 Answers