Search Unity

Asteroid Style Ship Movement?

Discussion in 'Scripting' started by Velcer, Apr 24, 2017.

  1. Velcer

    Velcer

    Joined:
    Mar 16, 2017
    Posts:
    24
    Atari Asteroids game
    http://www.freeasteroids.org/

    Does anyone know how to smoothly replicate the movement as seen in the above game without using rigidbody2d? I've tried many ways to do it and I can't get anything to work smoothly.

    Thanks in advance!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    For me, red flags go up when I hear, "can I do X without using Y?" It's certainly possible to implement velocity and momentum without using Rigidbody2D, and you could implement collision detection too, but at a certain point you're just reimplementing the class you were trying to avoid.

    Can you elaborate on why it is you want to avoid Rigidbody2D?
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I agree with StarManta. Your use case seems ideal for using a rigidbody2d.
     
  4. UziMonkey

    UziMonkey

    Joined:
    Nov 7, 2012
    Posts:
    206
    Especially since you're going to need a rigidbody for collision detection anyway and the movement is inherently physics based, I don't see why you would not want to use rigidbody. Look at AddTorque and AddForce, I've done this probably 10 times and it's trivial. Just AddTorque to turn and AddForce in the transform.forward direction and it all just works. Add some amount of drag to keep things controllable, maybe a tweak here and there, but the entire player controller for an asteroids style ship is all but a few lines of code.
     
  5. Velcer

    Velcer

    Joined:
    Mar 16, 2017
    Posts:
    24
    I'm making a multiplayer game, I want to check movement server side. not sure how that could be done with rigid body . I'm not using unity networking and the host will not be another user but rather my server.
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    OK, that makes a certain amount of sense. Still though, if you're checking stuff server side, reimplementing rigidbody (which is what you're talking about doing) means you're going to need to write code twice, right? Or do you expect to use the same code in a non-Unity environment?

    If you limit the rigidbody functions you choose to use, then writing something that can match it server-side shouldn't be difficult. Avoid stuff like AddForce, instead setting .velocity directly, for example. And definitely avoid all non-trigger collisions (an asteroids-type game doesn't need them, anyway). You can get pretty fine-grained control over the way rigidbodies behave.

    Replicating this server-side should be easy as long as you avoid the more complicated functions. FixedUpdate runs every Time.fixedDeltaTime seconds (default 0.02), and within that, you just add (velocity * Time.fixedDeltaTime) to the position, and that's like... most of the work. Then you'll want to sync velocity to your server in addition to position, and make sure that velocity isn't changing by more than your ship's acceleration allows, etc.