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

Problem with transform.position += rotation * new Vector3(0,0,1)

Discussion in 'Scripting' started by Findo, Jun 24, 2014.

  1. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    ^Title.


    Summary: I have a game where you build space ships and fly them around!
    Problem: When my ship is over 180 degrees, it goes backwards instead of forwards.
    Script:

    [//QUOTE//]transform.parent.position += transform.rotation * transform.parent.rotation * new Vector3(0,0,Thrust) * Time.deltaTime;[//QUOTE//]


    Hope that is code tags ^


    Anyways, so there is a thruster that controls the ships speed, and a variable, Thrust, which is 10


    The thruster is inside the ship, and sideways thrusters rotates the ship.
    So, I need it to times the transforms rotation AND the parents rotation.

    Any help is appreciated!
    Feel free to ask questions if you do not understand
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    This is because an angle of 180 degrees is the same as -180, 540, 900, -1080, etc...

    You can't use transform.rotation because the angles will never go above 180. You have to track it seperately.
     
  3. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    How would I do that?
     
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Wherever you set the rotation, set a float or Vector3 variable as well. Then use that variable instead of the transform.rotation.
     
  5. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Or is the rotation from a rigidbody? The problem I see then is that if you rotate your vector and it points backwards... and that's wrong? What do you want it to do instead?
     
  6. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    Wait what...?
    It's normal rotation from transform.
     
  7. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Sorry, I might be wrong? You know what... that Quaternion * Quaternion * Vector3 there, I'm not sure what that means in 3D space. I'm gonna bow out of this topic now because I'm not too familiar with Quaternions and I don't have enough time to study what transform.rotation * transform.parent.rotation does.
     
    Magiichan likes this.
  8. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    Now I'm getting this error when I made a Vector3 variable storing the rotation.
    Assets/Scripts/Building.cs(152,194): error CS0019: Operator `*' cannot be applied to operands of type `UnityEngine.Vector3' and `UnityEngine.Vector3'
     
  9. Conan_cs

    Conan_cs

    Joined:
    Apr 5, 2014
    Posts:
    16
    When you just want to rotate the object why you don't use the Rotate(...) methode from Transform?
    Code (CSharp):
    1. transform.Rotate (new Vector3 (x, y, z));
    When you want to translate the position depend on rotation you must use this syntax:
    Code (CSharp):
    1. static Vector3 operator *(Quaternion rotation, Vector3 point);
    you could solve it this way:
    Code (CSharp):
    1.  Vector3 absoluteDirection = transform.rotation * new Vector3(0,0,Thrust);
    2.         transform.position += absoluteDirection * Time.deltaTime;
    fount: http://docs.unity3d.com/ScriptReference/Quaternion-operator_multiply.html
     
    Last edited: Jun 24, 2014
    Magiichan likes this.
  10. Findo

    Findo

    Joined:
    Mar 11, 2014
    Posts:
    368
    Conane I did use the .Rotate method.
     
  11. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    I also do not fully understand the math (I believe I once did, a long time ago) but thankfully that's not a big deal because in a practical sense it's actually really simple to grasp:

    Multiplying a quaternion by a direction vector results in a new direction vector equivalent to the original vector rotated by the quaternion.

    That's pretty wordy, but basically it means that if you want to rotate a direction vector by a quaternion rotation, you go:
    Code (csharp):
    1. rotatedDirectionVector = rotationQuaternion * originalDirectionVector;
    I think the issue here is actually a misunderstanding between local and world space. transform.rotation gives the rotation in world space already, so I can't see any reason to multiply it by parent.transform.rotation except if you're unaware that it's already taken into account. If you do want the local rotation then you should use transform.localRotation, instead.
     
  12. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    I know a little bit about Quaternions and matrix math. Mostly that the order of multiplication matters...

    Do you know this?

    If I multiply a Quaternion and a Vector3, the result is a rotated Vector3.

    If I multiply a Quaternion, another Quaternion, and then the Vector3, is this the exact same thing as applying the first rotation, then applying the second rotation to that rotated Vector3?

    EDIT: I assume that multiplying a Quaternion and a Vector3 is a normal matrix product and there aren't some weird cast or operator overload going on somewhere.
     
  13. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,500
    Off the top of my head, no, I don't know the answer to that. I'd check that kind of thing if/when I have to use it. Using multiple rotations at once isn't something I commonly do, and I don't get excited over math for the sake of math, so it's not something that sticks around in my head for long.