So I have a bunch of people objects in my game, walking around the game board using the NavMesh components (random points are chosen throughout the game board and the people objects walk to those destinations using the NavMesh).
I also have another object that can fly around and hit the people, and when this object his a person I want that person to kind of go flying a bit, in a ragdoll sort of way.
I thought this would be simple with Unity physics but I cannot get this to happen.
I did a bit of searching and it seems there’s some considerations when using NavMeshAgent and Rigidbody/physics: https://docs.unity3d.com/Manual/nav-MixingComponents.html
So I tried just turning “isKinematic” to TRUE on my people prefabs but that didn’t do anything.
Does anyone have any ideas of what’s wrong? I’ve included my relevant code below.
Person.cs
```csharp
** public NavMeshAgent NavMeshAgent;
public Vector3 CurrentDestination;
public Rigidbody rigidbody;
private void Start()
{
path = new NavMeshPath();
NavMeshAgent = GetComponent<NavMeshAgent>();
rigidbody = GetComponent<Rigidbody>();
// move to random location on the game board
if (CurrentDestination != null)
{
bool pathFound = NavMeshAgent.CalculatePath(CurrentDestination, path);
//Debug.Log("Path found: " + pathFound);
NavMeshAgent.SetPath(path);
}
//Debug.Log("SetDestination for " + name);
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == “Ground” || collision.gameObject.tag == “Building” || collision.gameObject.tag == “Vehicle”)
{
//Debug.Log(string.Format(“Hit {0}”, gameObject.tag));
if (!hasPlopped)
{
hasPlopped = true;
// unimportant; just resets the flying object
StartCoroutine(DoAPlop(2));
}
}
else if (collision.gameObject.tag == "Person")
{
if (!hasPlopped)
{
hasPlopped = true;
hasPersonCollisionOccurred = true;
collisionPerson = collision.gameObject.GetComponent<Person>();
AudioSource audio = collision.gameObject.AddComponent<AudioSource>();
audio.PlayOneShot((AudioClip)Resources.Load("Explosion"));
// unimportant; just resets the flying object
StartCoroutine(DoAPlop(4));
}
}
}**
** **Plops.cs (this is the object that falls from the sky and hits the person)** **
csharp
**private void FixedUpdate()
{
if (!hasPlopped)
{
Vector3 forward = Camera.main.transform.forward;
forward.y = 0;
// these velocity calcs and the rigidbody.velocity assignment below are for the
// player to control the falling object by swiping
// i don't think this is a problem
Vector2 currentVelocity = new Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
Vector2 targetVelocity = Vector2.MoveTowards(currentVelocity, RotateVector(ScreenSpaceController.Instance.GetVector(), Vector3.SignedAngle(forward, Vector3.forward, Vector3.up)) * MaxHorizontalVelocity, MaxAcceleration * Time.deltaTime);
rigidbody.velocity = new Vector3(targetVelocity.x, rigidbody.velocity.y, targetVelocity.y);
}
// a collision with a person has occurred
if (hasPersonCollisionOccurred)
{
if (rigidbody != null && collisionPerson != null)
{
Debug.Log("Person collision");
collisionPerson.NavMeshAgent.enabled = false;
collisionPerson.rigidbody.isKinematic = false;
collisionPerson.rigidbody.AddForce(0, 0, 10000, ForceMode.Impulse);
collisionPerson.NavMeshAgent.velocity = collisionPerson.rigidbody.velocity;
hasPersonCollisionOccurred = false;
collisionPerson.NavMeshAgent.enabled = true;
collisionPerson.rigidbody.isKinematic = true;
}
else
{
Debug.Log("Person rigidbody is NULL");
}
}
}**
```