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

How to get the direction of a projected velocity?

Discussion in 'Scripting' started by Nanako, Aug 5, 2015.

  1. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    I have a desired direction A, and i have a body which is moving at a certain velocity B which might be moving in that direction, or might be moving away.

    I need to know how much velocity towards the target, the body has. This seems simple enough to begin with;

    Vector3 C = Vector3.Project(B,A);

    That gives me a vector, great.
    But if i check C.Magnitude, it's always going to be a positive value. the magnitude tells me how fast towards OR away from the target direction my object is going. But that's not helpful. I need to know only how far towards. If magnitude could be negative, with a negative value indicating it's moving away, then that would be helpful. But it cannot.

    What can i do in this situation to get the information i need?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Nanako likes this.
  3. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    that was very helpful, thank you

    Roughly it works like this now:
    Code (CSharp):
    1. Vector3 C = Vector3.Project(B,A);
    2. float D = C.magnitude * Vector3.Dot(A.normalized, B.normalized);
     
  4. Necronomicron

    Necronomicron

    Joined:
    Mar 4, 2015
    Posts:
    108

    It seems it should be:
    Code (CSharp):
    1. float D = C.magnitude * Mathf.Sign(Vector3.Dot(A.normalized, B.normalized));
     
  5. angelonit

    angelonit

    Joined:
    Mar 5, 2013
    Posts:
    40
    Still useful! Just used this to make a function that clamps the velocity in the relative axes of my character:


    public void ClampVelocity(Vector3 v3Min, Vector3 v3Max)
    {
    Vector3 v3Sign = new Vector3(Mathf.Sign(Vector3.Dot(velocity, charTransform.right)), Mathf.Sign(Vector3.Dot(velocity, charTransform.up)), Mathf.Sign(Vector3.Dot(velocity, charTransform.forward)));

    velocity = (charTransform.right * Mathf.Clamp(v3Sign.x * Vector3.Project(velocity, charTransform.right).magnitude, v3Min.x, v3Max.x)) + (charTransform.up * Mathf.Clamp(v3Sign.y * Vector3.Project(velocity, charTransform.up).magnitude, v3Min.y, v3Max.y)) + (charTransform.forward * Mathf.Clamp(v3Sign.z * Vector3.Project(velocity, charTransform.forward).magnitude, v3Min.z, v3Max.z));
    }

     
    Last edited: Nov 1, 2021
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    WOW!

    If you have more than one dot (.) on a line, you're just being mean to yourself.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    Break it up, practice social distancing in your code, one thing per line please.
     
  7. angelonit

    angelonit

    Joined:
    Mar 5, 2013
    Posts:
    40
    Modified because the "v3Sign" multiplier has to be inside the clamp