Search Unity

Tilting a spaceship

Discussion in 'Physics' started by RogueStargun, Jul 11, 2020.

  1. RogueStargun

    RogueStargun

    Joined:
    Aug 5, 2018
    Posts:
    296
    So I have a rogue squadron style spaceship controller which is working good and well. Every fixed update it runs the following:

    Code (CSharp):
    1.    
    2. Roll = -InputAxis.x * rollRate * dT;
    3. protected virtual void ApplyForces()
    4. {
    5.      rb.AddRelativeTorque(new Vector3(Pitch, Yaw, Roll), ForceMode.VelocityChange);
    6.       rb.AddRelativeForce(new Vector3(Strafe, Lift, Thrust), ForceMode.VelocityChange);
    7. }
    8.  
    Now my issue is, if I set the roll to be anything but a number close to 0, if the ship rolls too much my roll will be too severer.

    Say I'm moving the joystick left --> the ship rotates, now moving left is moving the ship down in space.
    So the simple solution is to simply set roll to 0.

    Now what I want to do is to add some visual flair. Instead of actually rolling the ship, I put the colliders and visuals of the ship in a child gameobject with the transform `bankTransform` and to make it look like the ship is tilting in every update I run:
    Code (CSharp):
    1.  
    2. bankTransform.localRotation = Quaternion.Euler(0f, 0f, maxBankDegrees * -InputAxis.x);
    3.  
    This code works and has the effect I desire (visual tilting the ship in response to controller inputs; without actually rolling the ship and altering the axis of control.

    The problem is that I am manually altering the rotation of colliders that are attached to a rigidbody. I believe this has a significant performance cost right (since I'm not using rb.AddRelativeTorque)?

    I can't seem to think of a good way to do this visual tilt using rb.AddRelativeTorque() as I want to tilt the ship without altering the axis of movement of the ship. Anybody got some clever ideas?
     
    Last edited: Jul 11, 2020
  2. RogueStargun

    RogueStargun

    Joined:
    Aug 5, 2018
    Posts:
    296
    TLDR how do I rotate a non-kinematic rigidbody along a local axis transform axis without screwing up my controls?
     
    Last edited: Jul 11, 2020