Search Unity

How do I get a vector in relation to another vector?

Discussion in 'Scripting' started by imphenzia, Sep 25, 2011.

  1. imphenzia

    imphenzia

    Joined:
    Jun 28, 2011
    Posts:
    413
    I have a spaceship (hollow arrow in picture) that is travelling with a velocity (dotted line).

    What I need to do is get the vector of the black solid line. I don't quite know how to describe it or what it is called =)

    Some examples (see attached picture):
    (A) When the ship is facing in the direction of the velocity vector I want the velocity
    (B) As I have turned 45 degrees to the left, I want the partial vector based on the direction I am facing
    (C) If I turn a little another 20 degrees I want a smaller piece of the vector (black arrow is my estimation)
    (D) When I come 90 degrees the spaceship is sliding completely sideways and I want vector zero
    (E) If I still continue to turn 45 degrees I want the equivalent magnitude (B) but facing backwards from my spaceship

    Does anyone know what this phenomenon is called and how I get this vector? =)
     

    Attached Files:

  2. boblol

    boblol

    Joined:
    Aug 12, 2009
    Posts:
    12
  3. imphenzia

    imphenzia

    Joined:
    Jun 28, 2011
    Posts:
    413
    Thanks - that sounds about right.

    So for my spaceship I have my transform.rotation (the direction in which my spaceship is facing) and transform.velocity (my speed and direction of travel).

    I was never any good in mathematics - how do I get my ship rotation into the second onNormal vector that is needed for the Vector3.Project function?

    Vector3 myNewVector = new Vector3.Project(transform.velocity, XXXX)

    What do I replace with XXXX with? =)
     
  4. imphenzia

    imphenzia

    Joined:
    Jun 28, 2011
    Posts:
    413
    Managed to solve after some guesswork and testing =) Thanks for pointing me in the right direction.

    Vector3.Project nearly got me the right result when using Vector3.Project(firedBy.transform.rigidbody.velocity, transform.forward) but I then used the magnitude to apply the speed to the fired bullet and magnitude isn't signed so it wasn't quite the right approach.

    This is what I came up with in the end... simple for those who understand vectors better than I do probably:
    Code (csharp):
    1. // Velocity for my bullet
    2. _velocity = (velocity + Vector3.Dot(firedBy.transform.rigidbody.velocity, transform.forward)) * transform.forward;
    You may ask why I simply don't use _velocity = (velocity + firedBy.transform.rigidbody.velocity) * transform.forward because that is technically the correct thing to do. The problem is that my space ships have quite heavy drag so they don't glide too far when you turn whereas the bullets have no drag at all (they are not rigidbodies) so it looked really strange. I've just tested the code above and it looks much better...

    Of course it isn't realistic - but then again, what in a space game is? Would be boring without sounds for example :)