Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Scripting a Mouse-driven Spaceship Controller

Discussion in 'Scripting' started by UnityFledgling, Aug 12, 2023.

  1. UnityFledgling

    UnityFledgling

    Joined:
    Jan 30, 2022
    Posts:
    20
    I want to control a 3D spaceship as it travels through space by using the mouse cursor's XY position across the game screen.

    When the scene starts, the ship is at a complete stop facing the Z forward direction before accelerating up to its top thrustSpeed.

    The ship's turning, banking, and pitching angles would automatically rotate allowing the ship to change its heading to follow the cursor's current position as it moves.

    I also want the ship to roll to the left when the Q key is pressed and to roll to the right when the E key is pressed. And, the ship would decelerate when the Spacebar is pressed until it comes to a complete stop. When the Spacebar is released, the ship would accelerate again up to the ship's maximum thrustSpeed.

    Does anyone know of a tutorial that covers this functionality?

    I have watched a lot of YouTube spaceship controller videos but I have not found any that cover this type of functionality.

    Thank you for your help.
     
    Last edited: Aug 12, 2023
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Kurt-Dekker and UnityFledgling like this.
  3. UnityFledgling

    UnityFledgling

    Joined:
    Jan 30, 2022
    Posts:
    20
  4. UnityFledgling

    UnityFledgling

    Joined:
    Jan 30, 2022
    Posts:
    20
    I'm still looking for a tutorial that effectively creates this functionality.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    You probably won't find one.

    Here's a good starting point for this... take it away!

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. // @kurtdekker - simple 3D spaceship controller
    4. // To use:
    5. //    - Slap this on your main camera
    6. //    - Press Play
    7. //    - Wiggle mouse to steer
    8. //    - Go where no human has gone before
    9. //    - Enjoy
    10. // Barf bag located in the seat pocket in front of you.
    11.  
    12. public class Demo3DSpacecraftPlayer : MonoBehaviour
    13. {
    14.     // increase if you feel the need
    15.     float speed = 5;
    16.  
    17.     void Update ()
    18.     {
    19.         float pitch = Input.GetAxis( "Mouse Y");
    20.         float roll = Input.GetAxis( "Mouse X");
    21.  
    22.         // increase to make things sportier
    23.         pitch *= 20;
    24.         roll *= 20;
    25.  
    26.         transform.Rotate( Vector3.forward * roll);
    27.         transform.Rotate( Vector3.right * pitch);
    28.  
    29.         transform.position += transform.forward * speed * Time.deltaTime;
    30.     }
    31. }
    Try this guy's approach to changing things one tiny step at a time:

    Imphenzia: How Did I Learn To Make Games:

     
    UnityFledgling likes this.