Search Unity

Rotating a Capsule

Discussion in 'Getting Started' started by MrBAlderson, Apr 5, 2021.

  1. MrBAlderson

    MrBAlderson

    Joined:
    Mar 28, 2021
    Posts:
    3
    Hi there,

    I'm currently prototyping movement for my first project. I have a spaceship modelled by a capsule. I'm using

    Code (CSharp):
    1. playerRb.AddRelativeForce(Vector3.forward * forwardInput);
    to apply a force in the direction of the front of the ship. The issue it, the way the capsule is set up "forward" is moving the ship sideways.

    I realise that I can fix this by using Vector3.left instead, but I'm wondering if there is a way to redefine the capsule so that, if this is my capsule: (=====), forward is to the right instead of up (as it is drawn in this post). Changing the scale doesn't give a capsule looking shape and rotating also rotates the "forward" direction. Is there any way for me to do this? I'd rather not use "left" for "forward" as it's going to lead to confusion when I replace the cylinder with the proper model.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yes: create an empty GameObject, and put your cylinder inside that, oriented properly so that the parent object's +Z is forward. Put the Rigidbody and all scripts on the parent object — the cylinder should be nothing but the mesh renderer, and maybe a capsule collider. It's just the model, not the game object!
     
  3. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    551
    Rotating wont change "forward" direction in your case because Vector3.forward is a global space vector which isnt aware of object rotation. To get directions relative to the object, use shortcuts of the Transform component such as Transform.right, Transform.up and Transform.forward
     
  4. MrBAlderson

    MrBAlderson

    Joined:
    Mar 28, 2021
    Posts:
    3
    That worked! Thank you :)
     
    JoeStrout likes this.