Search Unity

How do i rotate the space ship?

Discussion in '2D' started by Cuervito, Dec 2, 2019.

  1. Cuervito


    Dec 2, 2019
    hi im new to unity! so im a noob trying to learn. I want my spaceship to move foward and backwards with up and down arrow, and to rotate left with the left key and right with the right arrow key. i have tried with quaternion and with getkeydown and cant seem to get it to work, or it justs rotates 180 degrees at once.
    for the meantime i have so it moves without rotating but it just looks wrong. would apreciate the help. thanks!
    Code (CSharp):
    2. [ATTACH]522737[/ATTACH] void Update()
    3.     {
    4.         if(Input.GetKey(KeyCode.LeftArrow))
    5.         {
    6.             Vector3 position = this.transform.position;
    7.             position.x--;
    8.             this.transform.position = position;
    9.         }
    10.         if(Input.GetKey(KeyCode.RightArrow))
    11.         {
    12.             Vector3 position = this.transform.position;
    13.             position.x++;
    14.             this.transform.position = position;
    15.         }
    16.         if (Input.GetKey(KeyCode.DownArrow))
    17.         {
    18.             Vector3 position = this.transform.position;
    19.         }
    20.         if (Input.GetKey(KeyCode.DownArrow))
    21.         {
    22.             Vector3 position = this.transform.position;
    23.             position.y--;
    24.             this.transform.position = position;
    25.         }
    26.         if (Input.GetKey(KeyCode.UpArrow))
    27.         {
    28.             Vector3 position = this.transform.position;
    29.             position.y++;
    30.             this.transform.position = position;
    31.         }
    32.         if (Input.GetKeyDown(KeyCode.Space))
    33.         {
    34.             print("1");
    35.             FireBullet();
    36.         }
    38.     }
  2. jbnlwilliams1


    May 21, 2019
    Well, I am very new to Unity as well, but i do not see anywhere in your code were you are setting a rotation value, just a position..

    Maybe something like

    if (Input.GetKey(KeyCode.LeftArrow)){
    if (Input.GetKey(KeyCode.RightArrow)){
    Again, I am new, so code may not be exact.
    Last edited: Dec 3, 2019
  3. eses


    Feb 26, 2013
    Quick google search with "unity rotate object" gives you many alternatives and tutorials.

    You should add to your current rotation, using (usually) delta time and your rotation speed as multiplier.

    Same goes for your position, now you are just adding or reducing 1 unit when a key is pressed, definitely something you don't want unless you want to move step by step.

    In cases where you want to combine both rotation and moving forward (relative to current heading) you should move along your transform forward axis, not by just adding to one of the position axes (x, y or z) which will make your character/ship move only along main world axes, like you do now.

    Last edited: Dec 3, 2019
  4. PuppyPolice


    Oct 27, 2017
    Sounds like you want something like the old Asteroid game

    something like this?

    if that is the case, a quick youtube search gave me a playlist of a guy who makes an asteroid game from scratch in unity that might help you out