Question by
JeffHardddyyy · Sep 22, 2017 at 12:26 AM ·
c#unity 5rotationrigidbodynavmeshagent
Question about NavMesh and Rigidbody
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!
Comment