0
\$\begingroup\$

I'm making a game with an endless mode where you have to dodge asteroids until you eventually die. Since the game could be endless, I wanted to avoid the coordinates issue mentioned here.

My player ship remains at 0,0,0. The asteroids spawn at the top of the screen and "fall" to simulate the player flying past them. The player can also accelerate and decelerate. I have tried scripting asteroid movement, but now I would like to try using Unity's physics to simplify code.

When accelerating/decelerating I could add force to all existing asteroids, but then asteroids which haven't spawned yet will not reflect the player's current speed. I basically want all asteroids to have the same speed.

I was thinking maybe I could create a dummy physics object that I can apply forces to, that also keeps track of its speed. It would be great if all objects could just reference this dummy object's speed and forces. Is something like this possible with Unity's physics? Or is there a better way to do what I'm attempting?

\$\endgroup\$
  • 1
    \$\begingroup\$ Another often cheaper solution here is to periodically recenter the player and everything in the world, anytime the player leaves a conservatively-sized buffer around the origin. You may find this is simpler to orchestrate than fudging the velocities of all other objects. \$\endgroup\$ – DMGregory Mar 29 at 11:43
1
\$\begingroup\$

I ended up changing my implementation based on DMGregory's comment. I apply forces on the player, spawn objects ahead of them, and just recenter all the objects if they get too far in coordinate space. It seems to be the route with the least moving parts.

To answer my own question about sharing physics, I used something similar to Philipp's answer.

I couldn't just assign an asteroid's RigidBody to the dummy RigidBody to get the effect I wanted. However, forces added to the dummy object's RigidBody change its velocity. The asteroids can simply copy that velocity in FixedUpdate, so they will always be updated with the result of forces on the dummy object.

\$\endgroup\$
0
\$\begingroup\$

If instead of adding forces you play with the gravity in the physics maybe you could make it easier.

You can change gravity of the engine via script by:

Physics.gravity = Vector3(0, -1.0, 0);

But the visual effect is not the same as adding forces.

\$\endgroup\$
0
\$\begingroup\$

I assume you have the current "virtual" speed of the player in some variable? If that is the case, you can simply add its current value to the rigidbody.velocity of every asteroid the moment you spawn it. Those asteroids which already exist could be updated with rigidbody.AddForce whenever the player accelerates or decelerates. But keep in mind that the acceleration is independent of the mass of the asteroid, so you might also want to modify the velocity here instead.

Yes, the documentation tells you to not rely on changing the velocity property too much because it might result in unrealistic physics, but your physics model is unrealistic to begin with.

\$\endgroup\$

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.