0
\$\begingroup\$

I'm trying to move a kinematic rigidbody (a spaceship) with this code: float xSpeed = 0.0f; float ySpeed = 0.0f; float zSpeed = 30.0f; Vector3 moveDirection = transform.right * xSpeed + transform.up * ySpeed + transform.forward * zSpeed; ShipRigidBody.MovePosition(ShipRigidBody.transform.position + moveDirection * Time.deltaTime);

but moving the ship using ShipRigidBody.MovePosition makes it go through other objects with kinematic rigidbody. The collision detection of both objects is continuous dynamic and works for both other rigidbody objects and the object with the character controller.

I've tried not using a kinematic rigidbody for the ship but when it gets hitted by projectiles (with rigidbody) it moves and starts spinning around.

So my questions here would be:

  • Should I use a rigidbody for the NPC ship or use a character controller?
  • There is any way to prevent a rigidbody from move after collision of other rigidbody, besides making it kinematic?
  • How can I move a kinematic rigidbody so it doesen't go through other kinematic rigidbodies?

Thanks for your help!

\$\endgroup\$
2
\$\begingroup\$

If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore.

As explained in rigidbody documentation page, when the property isKinematic is set to true, collisions won't be checked anymore.

There is something important to understand: the role of the Rigidbody and the one of the Collider. Rigidbody gets all the Physics propeties while Collider is in charge of giving your object a shape in the physical world. A GameObject that would be composed of a Rigidbody only will have gravity applied to it (it will fall) but as it doesn't have a physical shape (a Collider) it won't collide with any other object... so if you have a plane under it, it won't stop it in its fall. There are some cases when you want to move a Rigidbody or rotate it by code, without having the gravity applied or any other forces... this is when you set your Rigidbody as kinematic.

  • When to use a rigidbody?

It depends on what you're trying to achieve. From what I get of your game with in your question, there are NPCs, and collision check and also projectile check. You can use a Rigidbody on every of these things... the restriction is more about "what do you want to do with these?".

  • How can I move a kinematic rigidbody so it doesen't go through other kinematic rigidbodies?

Kinematic rigidbodies will never compute collisions. So this is not the path you want to follow. I would suggest you to have a look at most of the Unity tutorials. There, it's explained that you should use AddForce on Rigidbody if you want to move it in a direction.

your_rigidbody.AddForce(moveDirection * thrust);
  • Spinning Rigidbody on projectile impact

It's a classic, your projectile has a Rigidbody and so does your NPC. So when they collide they both start turning around and spinning like drunk fighters. You don't need to have an active collider on your projectile most of the time. You usually just want to know when a projectile has hit a NPC and then make the projectile disappear and start the rest of the game logic (apply damages, create a visual fx...). Just define your projectile Collider as a trigger and it will not collider with other rigidbodies anymore but -the method OnTriggerEnter will be called (instead of OnCollisionEnter) to let you know when another Collider has entered your trigger. It will prevent your object to spin but still warn you about collisions.

  • Side note

As you seem to have NPCs and want to have them moving in your scene and interact with things... I would suggest you to have a look to NavMeshAgent feature of Unity. Very well explained here.

\$\endgroup\$
  • \$\begingroup\$ Thanks for your answer! I tried doing as you said, and works well for me, except when the ship crashes when another. In that case still I have the spinning issue, and i can't do them a trigger. \$\endgroup\$ – Hector Oct 22 at 13:40
  • \$\begingroup\$ Well, that'd be the "normal" behaviour of two colliding rigidbodies. Note that in the RigidBody component you can add rotation constraints to avoid having them spinning ;) \$\endgroup\$ – lvictorino Oct 22 at 13:42
  • \$\begingroup\$ Oh, I thought that the rotation constraints would also affect the ship's rotation, but fortunately they don't (at least not when using transform.rotation). Thanks! By the way,you told me to take a look at NavMeshAgent. Thanks for the suggestion, but I don't think It's going to work in a full 3D scenario, like a space game. Probably I'll try some A* approach. \$\endgroup\$ – Hector Oct 22 at 15:24
  • \$\begingroup\$ Oh. Yep... Then the navmesh is not what you're looking for. \$\endgroup\$ – lvictorino Oct 22 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.