Search Unity

Help with 3D Spaceship movement script

Discussion in 'Scripting' started by Spacejet13, Jan 18, 2019.

  1. Spacejet13

    Spacejet13

    Joined:
    Apr 22, 2018
    Posts:
    8
    Hi, so as the title suggests, I am trying to make make a movement controller script for a spaceship. I have made 3rd person player controllers before, but this is totally unlike anything I have done before and I would like some help with it. I want to detect collisions so I am using rigidbody.AddForce() (just a disclaimer)

    So the thing is that i have no idea how to implement it. I'll post the code I am working on so you can see where I am. My goal would be to have a controller similar to the one seen in No Man's Sky. Any tips and Help is greatly appreciated.

    Ps. By the way, I am using a scriptable object, Spaceship, to store data for different types of spaceships.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class SpaceshipMovement : MonoBehaviour
    4. {
    5.     public Spaceship spaceship;
    6.  
    7.     private float thrust = 0;
    8.     public float mouseSensitivity;
    9.  
    10.     public Rigidbody rb;
    11.  
    12.     private void Update()
    13.     {
    14.  
    15.         float tiltx = -Input.GetAxis("Vertical") * Time.deltaTime * spaceship.handling;
    16.         float tilty = Input.GetAxis("Horizontal") * Time.deltaTime * spaceship.handling;
    17.  
    18.         thrust += Input.GetAxis("Thrust") * Time.deltaTime * spaceship.acceleration;
    19.         thrust = Mathf.Clamp(thrust, spaceship.minThrust, spaceship.maxThrust);
    20.  
    21.         Debug.Log(thrust);
    22.  
    23.         Vector3 keyboardRot = new Vector3(tiltx, tilty, 0);
    24.  
    25.         transform.Rotate(keyboardRot);
    26.     }
    27.  
    28.     private void FixedUpdate()
    29.     {
    30.         if (thrust < 5 && thrust > 0)
    31.             rb.drag = 2;
    32.         else if (thrust == 0)
    33.             rb.drag = 5;
    34.         else if (thrust > 5)
    35.             rb.drag = 0;
    36.  
    37.         rb.AddRelativeForce(new Vector3(0, 0,  thrust));
    38.     }
    39. }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You haven't stated what the problem is.
     
  3. Spacejet13

    Spacejet13

    Joined:
    Apr 22, 2018
    Posts:
    8
    I just wanted to get some tips on how to go about creating the controller.
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Well it seems you're off to a fine start. Getting the exact movement of your character (or ship in this case) just right is going to take time and a lot of iterations, but it's not really something we can help with as it's more about tweaking small numbers and adding minor adjustments. If you're stuck on any major feature, like barrel rolls or something, that's something we can definitely help with.
     
  5. Spacejet13

    Spacejet13

    Joined:
    Apr 22, 2018
    Posts:
    8
    Something that is happening that I would appreciate help with is that that I cannot change my direction suddenly. What I mean by that is that If I am going straight up, and i sharply turn down, then it doesn't change the direction immediately, as I am using rigidbody.AddForce(). What should I do to get that right?
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,775
    Adding forces normally requires time, to go from initial state to next state. Also, consequences are angular and linear velocities, which need be taken into account.

    But still your problem description is too vague for me.
    From your description, I can not visualise the issue you are having.
     
  7. Spacejet13

    Spacejet13

    Joined:
    Apr 22, 2018
    Posts:
    8
    Ok, so I changed things up a bit in my code. I am now using transform.position instead of rigidbody.AddForce(). This fixed the problem, but I think this will hinder with collision detection. I would really love to use rb.addforce, but the issiue is that whenever i change direction, the ship doesn't it keeps going the direction it was going in before. only after overcoming the older force that was pushing it in the wrong direction does it move in the desired direction. Now, though switching to transform.position changed it, but the roll effect does not apply. what i mean by that is even if i roll to the left, the plane does not curve that way, instead it keeps going the it was before, i.e, straight. How do I simulate this effect?

    My Code:
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. public class SpaceshipMovement : MonoBehaviour
    5. {
    6.     public Spaceship spaceship;
    7.  
    8.     private float thrust = 0;
    9.  
    10.     private void Update()
    11.     {
    12.         CalculateRotation();
    13.         CalculateThrust();
    14.     }
    15.  
    16.     private void CalculateRotation()
    17.     {
    18.         float pitch = -Input.GetAxis("Vertical") * Time.deltaTime * spaceship.handling;
    19.         float yaw = Input.GetAxis("Horizontal") * Time.deltaTime * spaceship.handling;
    20.         float roll = -Input.GetAxis("Roll") * Time.deltaTime * spaceship.handling;
    21.  
    22.         Vector3 keyboardRot = new Vector3(pitch, yaw, roll);
    23.  
    24.         transform.Rotate(keyboardRot);
    25.     }
    26.  
    27.     private void CalculateThrust()
    28.     {
    29.         thrust += Input.GetAxis("Thrust") * Time.deltaTime * spaceship.acceleration;
    30.         thrust = Mathf.Clamp(thrust, spaceship.minThrust, spaceship.maxThrust);
    31.  
    32.         Debug.Log(thrust);
    33.  
    34.         transform.position += transform.forward * thrust * Time.deltaTime;
    35.     }
    36. }
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,775
    You probably want add more drag and add continuous relative force, if you want effect as if driving the car, rather than flying spaceship. That is to reduce side skidding effect.

    Alternatively, you can simulate skidding drag, with relevant side way force.
    Dot product could be use here.
     
  9. Spacejet13

    Spacejet13

    Joined:
    Apr 22, 2018
    Posts:
    8
    oh, so while you use the rigidbody to make movements, you need to consider drag and angular drag? Basically, you need to simulate real world physics in-game, right? How would you do that though? I looked at the AeroplaneControler script in standard assets but understood practically none of it..
     
  10. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,775
    RigidBody has drag properties, so you can start of with playing with these values. Run some test. Make prototype. Try understand airplane example. Dissects it, and change/ add bits.
     
  11. Spacejet13

    Spacejet13

    Joined:
    Apr 22, 2018
    Posts:
    8
    Ok. Thanks for the fast reply. I actually started using AddRelativeForce and ForceMode.Acceleration, and as it ignores mass, its doing a pretty good job.