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

Opposite velocity vector to target vector

Discussion in 'Scripting' started by Karwoch, Jan 11, 2016.

  1. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    111
    I have a gameobject with velocity vector3 v1 and also a vector3 to target t1. Now I need a vector which when I add to v1 will result in t1 - I only need a direction, magnitude of it doesn`t matter (I can count it easly).

    So it is somewhat mirror of vector v1 throught t1, but I have no idea how to do it in 3d space (in 2d I think I could handle it). I appriciate any help, as my vector based imagination is pretty low...
     
  2. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Either I'm misunderstanding your question, or you're asking about pretty basic math.

    x + v1 = t1
    x = t1 - v1

    Could you elaborate more on what the vectors are? Is t1 a unit vector?
     
    Karwoch likes this.
  3. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    111
    I tried this, but it doesn`t work as I thought. I have a missile which flyes in space, as space doesn`t have air nor gravity, all targeting stuff is calculated on basic vector math. So I have a missile with velocity vector, a vector to target, and what I need is now - a vector in which missile have to thrust in order to change it`s current velocity vector to target vector. Hope I manage to clear it some.
     
  4. Immanuel-Scholz

    Immanuel-Scholz

    Joined:
    Jun 8, 2013
    Posts:
    221
    Lets get there together:

    1. If you would add the inverse of your current velocity, you would stand still, right?
    2. Assume you would stand still. If you would then just add the the Vector towards your target (with magnitude as your desired speed), then you would have that velocity, right?

    Now, add both vectors together, and you get your vector that you need to add to your current velocity.

    x = -v1 + t1

    or simplier

    x = t1 - v1

    It that does not work, make sure that you actually add velocities here. Its something totally different then applying forces..
     
    Karwoch likes this.
  5. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Is the vector to target a unit vector direction, or is it the actual length?
     
  6. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    111
    Damn, I suck at math. Great, thank You very much, I already created few games, know programing, but these vector basic math is still get me headache.

    So thanks even more, as with every that kind of help I`m getting little better at this.
     
  7. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    111
    I normalize every vector for this purpouse, but vector to target is simply calculated by target position - my position. Now all I need is to find nice way to implement this, as as You sad, there is big difference between add force and just changing velocity vector.
     
  8. Immanuel-Scholz

    Immanuel-Scholz

    Joined:
    Jun 8, 2013
    Posts:
    221
    Delta vectors are seldom normalized, as their magnitudes represents the speed.

    Think of it this way:

    If you going with velocity v1 towards your current goal, and then apply -v1 * 1000000000 (on billion times into the opposite direction), then you are BLAZING backwards to where you came from. Now adding a puny-tiny normalized vector in your desired direction to this beast is not going to change your backward light-speed to any significant level.

    So no: you can't just normalize everything that looks like a vector :D
     
  9. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    111
    But I only count direction towards, as engine do the rest. To do some test, I turned off my engines, and all rotation scipts, and add those three lines:
    Code (CSharp):
    1. vectorToTarget =  (Target.transform.position - myTransform.position).normalized;
    2.  
    3.             myTransform.rotation = Quaternion.LookRotation(vectorToTarget - myRigidBody.velocity.normalized);
    4.  
    5.             myRigidBody.AddForce(transform.forward* 100000);
    So vector forward is just direrction, and add force do the rest. But my missiles just started to turn around themselfs, slightly moving in right direction.
     
  10. Immanuel-Scholz

    Immanuel-Scholz

    Joined:
    Jun 8, 2013
    Posts:
    221
    I assume you are calling these lines only once and not every frame, right?

    1. You add a force, not a velocity. You might want to use myRigidBody.AddForce(..., ForceMode.VelocityChange) or just call to "myRigidBody.velocity += ..."
    2. You are adding a force directly towards the target. Since you are probably already have a velocity in a certain direction (turning off the engines in space means "keep flying with current velocity"), you have to compensate for that change too, by substracting it from the desired velocity.

    In the end, you will get back to (assuming that the magnitude of "vectorToTarget" is the desired speed)

    myRigidBody.AddForce(vectorToTarget - myRigidBody.velocity, ForceMode.VelocityChange)

    or

    myRigidBody.velocity += vectorToTarget - myRigidBody.velocity;

    which of course is simply:

    myRigidBody.velocity = vectorToTarget
     
  11. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    111
    its in coroutine per frame - so theoritically my missile should in time get to targeted vector.
     
    Last edited: Jan 11, 2016
  12. Immanuel-Scholz

    Immanuel-Scholz

    Joined:
    Jun 8, 2013
    Posts:
    221
    Why the Quaternion.LookRotation? Just do this:

    Code (csharp):
    1.  
    2. vectorToTarget =  (Target.transform.position - myTransform.position).normalized;
    3. myTransform.LookAt(Target.transform);
    4. myRigidBody.velocity = vectorToTarget;
    5.  
    Also note, that the magnitude of the velocity is your speed. You normalize the vector, that means you are flying with 1 m/s (or whatever your units in your game are ;)). If your engine (who is supposed to "take care" of the speed as you said) try to alter the speed by applying forces later, that won't work, as you constantly resetting the velocity to 1 m/s with this code.

    Final note: You are manipulating velocities here... that might be ok for your game, but if it is some kind of "real world simulation", you can't just set a velocity of an object. In real world, engines always apply thrust (force).

    That's why I asked before whether you are doing velocities or accelerations here ;).

    PS: Basically, to calculate the force needed, multiply the desired velocity by the mass. More mass -> you need more force to change your speed.
     
  13. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    111
    Finally got it!! Big thanks to You. You are right, I do it by accelerations, just simplified it for test purpouses. My missiles still miss the target, but are now pretty close now (propably because rotating isn`t now instant, this is also done by adding rotation forces).

    My script:

    Code (CSharp):
    1. vectorToTarget =  (Target.transform.position - myTransform.position).normalized;
    2.             calculatedVector = (vectorToTarget - myRigidBody.velocity.normalized).normalized;
    3.  
    4.             rotationScript.targetDirection = vectorToTarget + calculatedVector;
    In here rotationScript try in time rotate missile in desired direction, and moveScirpt in missile always add force in forward direction. Now all I need is to recalculate final direction vector to include delays from rotating and it should work as intended. But now I will manage to do it by my own, big thanks!
     
  14. Immanuel-Scholz

    Immanuel-Scholz

    Joined:
    Jun 8, 2013
    Posts:
    221
    You seem to insist that you have to normalize everything that looks like a vector. I recommend a basic book about vector math to cure you of that illness :p

    But as long as you are happy...:rolleyes:
     
  15. Karwoch

    Karwoch

    Joined:
    Sep 16, 2014
    Posts:
    111
    As long as it works for a matter of fact ;D But if I do not do that, it just stop to work properly, so that is why I do it ;). God, to think I finished informatics on University... but way long time ago! ;)