Search Unity

help with my own space shooter!

Discussion in 'Getting Started' started by Stevew82, Oct 30, 2015.

  1. Stevew82

    Stevew82

    Joined:
    Sep 24, 2015
    Posts:
    11
    Hi all!

    After doing the Space Shooter tutorial, decided I wanted to put my own spin on things and try to complete my own little space shooter project (not really a coder so please forgive if I'm being a little vague). I am trying to make an asteroids style game where you can thrust your ship around the game space and rotate it. Here's the code I used in my PlayerController script in the Update function.

    void Update ()
    {
    rotation = Input.GetAxis("Horizontal") * shipRotationSpeed;
    rotation *= Time.deltaTime;

    translation = Input.GetAxis("Vertical") * shipSpeed;
    translation *= Time.deltaTime;

    transform.Rotate (0, rotation, 0);
    transform.Translate (0, 0, translation);

    }

    This code works, I found it in the Documentation more or less the way it is here, except the ship moves around like a little racecar and isn't very satisfying to me. My question is, how would I get it to 'thrust' and keep moving in the same direction? Would I have to use AddForce and put my code in the FixedUpdate function instead just manipulatung the transform values directly? If I'm not being specific enough, just say!

    Thanks for the help!
     
    Last edited: Oct 30, 2015
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    If I understand you correctly, your complaint is that your ship has no momentum. As soon as the control input goes to zero, the ship stops. Right?

    You could use physics for this, but I wouldn't. It's overkill and probably more to learn than just doing it yourself. It can also be harder to get exactly the effect you want.

    The transform already keeps track of position and rotation, but what's missing is velocity. You will need to add a velocity (a Vector3) property to your script. Then, your update method will do these steps:
    1. Use the vertical input to add to the velocity (something like transform.forward * shipSpeed * Time.deltaTime).
    2. Do any further adjustments you want to the velocity, such as capping it at some maximum speed (magnitude) or having it slowly decay to 0.
    3. Apply the velocity to the position of the ship (transform.position += velocity * Time.deltaTime).
    Now you should be able to scoot your ship around pretty much like a real spaceship!
     
    Stevew82 likes this.
  3. Stevew82

    Stevew82

    Joined:
    Sep 24, 2015
    Posts:
    11
    Yes. The ship feels like a go cart and stops fairly abruptly. Also thanks for replying though I don't fully understand your response. I got as far as making a Vector3 velocity = new Vector3(but didnt know what to put in here.)

    sorry for the noobish questions :S
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You don't put anything in there. Leave the parentheses empty. And make sure this is a property of your script, not a local variable.

    (If you don't know the difference between properties and local variables yet, that's OK, but you need to back up and do some basic programming tutorials first.)

    Then just take each of the steps above — steps 1-3 all go in the Update method — and try to convert my words into code. Post back if you get stuck!
     
    Stevew82 likes this.
  5. Stevew82

    Stevew82

    Joined:
    Sep 24, 2015
    Posts:
    11
    Thanks again! I'll get there in the end! :D
     
    JoeStrout likes this.
  6. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    A sideways approach might be to either make a copy of your project that you can play around with until you get it right without worrying about breaking what you already did (when you are happy you can make the final changes to the original) or start a new project & just use primitives to work on nothing but the movement so you only have the one script & object in your game scene.
     
  7. Stevew82

    Stevew82

    Joined:
    Sep 24, 2015
    Posts:
    11
    This is true. As I've stated the code I already have does work, it just doesn't 'feel' quite right. I think I may just be focusing too much on silly stuff right now when I could actually just get on with it and finish my little project first, and polish later (this is my first project)
     
  8. Stevew82

    Stevew82

    Joined:
    Sep 24, 2015
    Posts:
    11
    Settled on this solution, which works exactly the way I want it, though I'm not sure about using the transform to rotate, but was unsure how to achieve the same thing using a Rigidbody:

    public class PlayerControllerNew : MonoBehaviour
    {
    private Rigidbody rb;
    private float shipRotate;
    private float shipThrust;

    public float shipRotateSpeed;
    public float thrustValue;

    void Start()
    {
    rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
    shipThrust = Input.GetAxis("Vertical");
    shipRotate = Input.GetAxis("Horizontal") * shipRotateSpeed;
    shipRotate *= Time.deltaTime;

    if (shipThrust !=0)
    rb.AddRelativeForce(Vector3.forward * thrustValue);


    if (shipRotate !=0)
    transform.Rotate (0, shipRotate, 0);
    }
    }
     
    JoeStrout and tedthebug like this.
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yep, that'll work — that's using the physics engine, which I still feel is overkill for something like this, but if it works for you, hey go for it!

    One forum tip for the future: use the "Insert Code" button (just between the movie strip and the floppy disk icon) to insert code into your posts. It will keep the formatting and make it a lot more readable.

    Anyway, nice work, and good luck with the rest of your game!
     
    BrandyStarbrite likes this.
  10. Stevew82

    Stevew82

    Joined:
    Sep 24, 2015
    Posts:
    11
    It may seem like overkill, but it feels so much cooler :D
     
  11. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I would definitely use forces and torque to move a ship in asteroids. You can manipulate drag values to slow down the rotation and remove drag values so the ship continues to move in the direction travelling. The only issue you'll have to solve is edge wrapping.
     
    Stevew82 likes this.
  12. Stevew82

    Stevew82

    Joined:
    Sep 24, 2015
    Posts:
    11
    Yes, this is just a little hobby project, so Im trying to talkle one problem at a time. Edge wrapping had occurred to be and I'm not sure how I'm going to do that yet but it should be fun...i think :D