Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

transform.rotation to vector

Discussion in 'Scripting' started by freid, Sep 23, 2008.

  1. freid

    freid

    Joined:
    Mar 31, 2007
    Posts:
    60
    Hi guys,

    I guess its a very simple problem. :

    I instantiate a rocket :
    rocketClone : Rigidbody = Instantiate(rocket, MissilePos.transform.position, MissilePos.transform.rotation)

    then i want this rocket to follow the MissilePos.transform.rotation "angle".

    I though i could just use a vector3 (1,0,0) * MissilePos.transform.rotation

    But unity disagree. It says that i can't multiply vectors and quats, could you please help me ?
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Transform.rotation is the rotation of the transform stored as a quaternion, not a vector, and so the error is a correct one. What do you mean when you say you want the missile to follow the rotation angle? An angle isn't a direction, so how can it follow that? If we can get some clarity I'm sure we'll sort out a good solution.
     
  3. freid

    freid

    Joined:
    Mar 31, 2007
    Posts:
    60
    Thanks you HiggyB for trying to understand me.
    Yep sorry for being unclear. I mean that i want to align my rocket velocity direction, with my canon (here MissilePos.transform.rotation).

    My rocket "direction" or angle is good when created, but i want to move it now. I'm a math dummy, but a vector is often draw like an arrow right ? I want to align that arrow with my rocket rotation. I want to compute the right vector from the cannon's quat or the cannon orientation.
    I hope that now i'm understandable :oops:

    I try to explain again : my canon rotates, it fires rockets, i just want to instantiates rockets and shoot it with the same orientation as the canon (at the moment i use vector3.right for my velocity.)
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Use Vector3.forward to make it go in the direction it's facing.

    --Eric
     
  5. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    In fact, you can multiply vectors with quaternions, but it looks like it only works with the operands in a specific order. Quaternion * Vector3 is allowed, but Vector3 * Quaternion is not defined. The result of multiplying a vector with a quaternion is a vector which has been rotated by that quaternion.

    I think you mean transform.forward, which is equivalent to transform.rotation * Vector3.forward.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    No, not really...

    Code (csharp):
    1. rigidbody.AddRelativeForce(Vector3.forward*speed);
    For example, or

    Code (csharp):
    1. transform.Translate(Vector3.forward*Time.deltaTime*speed)
    It defaults to Space.Self, not Space.World. Although transform.forward is appropriate in some cases.

    --Eric
     
  7. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    Good point, although rigidbody.AddForce(Vector3.forward) obviously wouldn't give that result. I mentioned transform.forward because Vector3.forward can't really be described as 'in the direction you're facing' without more context.
     
  8. freid

    freid

    Joined:
    Mar 31, 2007
    Posts:
    60
    Thanks a lot guys,

    That helps !

    I was messing up AddRelativeForce and AddForce :)