Rigidbody physics on a walkable Spaceship

Hello All!

I would like some advice on a problem I am facing. The setup:
I have a Rigidbody spaceship, that should be able to collide with other objects in space, such as other spaceships. The spaceship should not use gravity either. However, the player should be able to walk around on the ship, and whilst on the ship, the player has normal gravity. The problem is if I don’t make the ship kinematic the player’s mass will cause the ship to fall.
If I make the ship kinematic, then collision does not work the same way anymore. I would like the ship to not be kinematic, so then I can use AddForce which I (think) is easier to use in this case than to calculate the forces manually and collision with other Rigidbodies is easier. Do you have advice on how I can make the ship non-kinematic, whilst still allowing the player to walk on the ship as if it is the regular ground? And allow for the ship to move around like a spaceship?

Another small problem, right now I make the transform of the ship the parent of the character when the character is on the ship. If the player stands still on the ship, his position should not change relative to the ship. However, this does not work as I expected too, since the player tends to slide off after a while.

Any help is much appreciated!

Hi,

Do you really need rigidbody physics for the space ships in space? What should for example happen when two ships collide? Should they bounce of from each other, while the player walks around on top of them securely held in place by some gravity force?

Hi,

Thanks for the reply!

The ships should both get damaged when they collide, the same goes for a ship colliding with an asteroid. I would also like the ships to bounce off from each other to some extent. Maybe it is possible to calculate this manually, but that does sound difficult.

You probably don’t need to simulate so much gravity in space. I would concentrate on how the damage should be visualized. Probably with some animations, and loose parts to let go from the ship. Assuming the ships always flies in the forward direction it should be easy to calculate the the change of direction on impact.

If you insist on having both player and the ship as physical objects, I think you could give a ship much greater mass than the player, so effect of the player on the ship physics is negligible. BTW, AddForce isn’t “easier to calculate”, it is a main method of affecting Rigidbody with forces. What matters for ease of use is ForceMode (second argument after your force vector input). ForceMode.Force, ForceMode.Impulse and ForceMode.Acceleration all take the mass of the object into account, while ForceMode.VelocityChange directly sets the desired velocity per second, regardless of mass.

What about making the character move with the ship, the ideal solution would be to add the velocity of the ship to the character every physic timestep. Though it is easier said than done. Since if the ship can rotate, then you’ll need to calculate this velocity based on relative position of the player to the ship.
The method that worked for me when I tried to make a platform solver is following: at the end of the physics timestep, store player global position, then calculate its local position relatively to the ship

prevLocalPos = shipTransform.InverseTransformPoint(playerTransform.position)

and store it as well. Then, on the following timestep, you can calculate the “predicted” position of the player based on ship movement

Vector3 newGlobalPos = shipTransform.TransformPoint(prevLocalPos)

Subtract the global player position on the previous frame from it and you’ll get the required velocity per physics timestep. Divide it to the Time.fixedDeltaTime if you need the velocity to be per second.

Vector3 addedVelocityPerSecond = (newGlobalPos-prevGlobalPos)/Time.fixedDeltaTime

Add this velocity to player’s own velocity to make it move with the ship. This doesnt account for the player’s rotation around its own Y axis though.

2 Likes

Those are some great tips @DimitriX89 , Thanks! I will look into this.
What I meant with “easier to calculate” is that with AddForce collision is handled, but with MovePosition it is not (as far as i know). That is why I would rather use AddForce

1 Like

Good point; remaking collision system is too much work