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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Calculate the direction from vectorA to vectorB

Discussion in 'Scripting' started by DrMrkev, Apr 17, 2016.

  1. DrMrkev

    DrMrkev

    Joined:
    Oct 25, 2015
    Posts:
    19
    Hello everyone, I make a little game that I am learning Unity3D,
    You are playing as ball and have gathered the cubes ...
    from a distance they could cubes into motion toward the players, but I need to calculate the direction from cube to player, I know that I could use Vector3.LookAt but I need a way to store variable for future action.

    does anyone know how to calculate this direction?
    Direction.PNG

    -DrMrkev
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
  3. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    The direction from A to B is:
    Code (CSharp):
    1. Vector3 direction = (B - A).normalized;
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    If he wants it as a veftor aswell, but if e want an angle, then he needs to convert this to polar coordinates
     
    metamorphist likes this.
  5. DrMrkev

    DrMrkev

    Joined:
    Oct 25, 2015
    Posts:
    19
    so if I understand it, this
    Code (CSharp):
    1. Vector3 dir = (B - A).normalized \\"I use  for direction variables names like dir, dir1, dirB etc."
    returns a Vector3 - the direction from point A to point B .......................
    WOW it is so easy ...
     
  6. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    No, its a vector wizh a length of 1 pointing in the direction of your gameobject. The difference is small but crucial
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The direction is just B - A. Don't bother normalizing, unless you actually need to.

    --Eric
     
    hippocoder likes this.
  8. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Its still a vector pointing in a direction.
    Difference
    The vector can be used to give force or velocity to that gameObject in the direction the other object is. Its very useful for programmers

    The direction can be used in some mathematical ways of calculating lengths, or in physics, forces. It is very useful for (you guessed it) mathematicians and physicians

    Its a subtle difference, but its crucial
     
    Z0leee87 likes this.
  9. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    I think Eric was just saying, there's no need to normalize unless you need it normalized. Since normalizing requires extra math (including a square root) that can be considered expensive when done repeatedly.

    Since a vector is defined as 'magnitude' and 'direction', and a normalized vector is just a vector whose magnitude is 1. Really both vectors (normalized or not) have the same information (direction), it's just the un-normalized one also has the magnitude (distance from a to b).

    Heck, it could be said normalzing actually loses information, which means you just spent processing power to LOSE information. Why do that unless you need to lose it?
     
    KelsoMRK and hippocoder like this.
  10. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Deltax = x2-x1

    Vector3 Direction = Destion - Source.
     
  11. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
  12. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    your point?

    linking random stuff doesn't really mean much.

    Note, I'm not approaching this from some random wiki article perspective. I'm coming at this from over 15 years worth of study and experience in math, physics, and game design. I'm fairly certain I know what a vector is, and its definition.
     
  13. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    lol, Since he's throwing some links at you, school him back with your links. Throw some space puppy in his face.

    https://github.com/lordofduct/space...lob/master/SpacepuppyBase/Geom/PhysicsUtil.cs