Nanako
August 5, 2015, 10:13am
1
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?
tells you towards / away as a +1/-1, multiple the magnitude by that.
1 Like
Nanako
August 6, 2015, 1:21pm
3
that was very helpful, thank you
Roughly it works like this now:
Vector3 C = Vector3.Project(B,A);
float D = C.magnitude * Vector3.Dot(A.normalized, B.normalized);
Nanako:
that was very helpful, thank you
Roughly it works like this now:
Vector3 C = Vector3.Project(B,A);
float D = C.magnitude * Vector3.Dot(A.normalized, B.normalized);
It seems it should be:
float D = C.magnitude * Mathf.Sign(Vector3.Dot(A.normalized, B.normalized));
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));
}
angelonit:
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 * v3Sign.x * Mathf.Clamp(Vector3.Project(velocity, charTransform.right).magnitude, v3Min.x, v3Max.x)) + (charTransform.up * v3Sign.y * Mathf.Clamp(Vector3.Project(velocity, charTransform.up).magnitude, v3Min.y, v3Max.y)) + (charTransform.forward * v3Sign.z * Mathf.Clamp(Vector3.Project(velocity, charTransform.forward).magnitude, v3Min.z, v3Max.z));
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.
angelonit:
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));
}
Modified because the “v3Sign” multiplier has to be inside the clamp