- Home /
Getting the velocity direction as a euler angle.
Hello peoples,
I need a way to get the direction than an object is travelling in, as a Euler angle. In fact it might be worth mentioning what I'm actually trying to do, just in case there is an easier way. ahem
I'm working on a little demo where your character has to avoid being blown off the level by the wind. I managed to implement the wind with a simple Character.transform.AddForce, but since this runs every cycle, the force acting on my character increases constantly. So my idea was to try and simulate what happens in real wind; divide the force added by the wind with the speed with which the character is already being blown, thus, they can never exceed the windspeed itself.
Sorry about that. Anyway, as far as I know I need to get the direction of movement for my character as a euler angle, so that I can compare this euler angle to the wind direction itself. I've heard of normalizing, but it doesn't seem to return a euler angle and I'm not sure how to change that.
Please help.
The velocity of your character is the direction you are headed in, normalize just brings the values between 0 & 1 while maintaining the same direction, so you can use that direction without having really large values.
I'm not too sure what you mean by as a euler angle, that's just a Vector3 but named for rotation.
In your case the velocity normalized should be what you're looking for. If you want to test it, you can draw a line from your character to his position plus the normalized velocity to see if it's showing the right direction.
void Update(){
Vector3 vel_n = yourVelocity.normalized;
Debug.DrawLine(transform.position, transform.position + vel_n, Color.red, 0, true);
}
Answer by SilentSin · Nov 28, 2013 at 03:36 AM
Trying to express velocity as a euler angle makes zero sense. Your problem can be solved, it just has nothing to do with euler angles.
The reason in real life that things don't keep getting faster from find isn't because the force being applied by the wind decreases, its because of drag.
If you give the rigidbody some drag, that will slow it down for you. As the object gets faster, drag will start to have more of an effect until eventually the acceleration from the wind force is being perfectly cancelled out by drag each frame, which would be your maximum speed. Calculating the max speed requires some relatively complicated math, I usually just tweak the drag value manually until it achieves the results I want.
Seems to work, except that my character falls at a slightly slower speed. But this isn't supposed to be anything fancy, so it'll do.
Answer by robertbu · Nov 28, 2013 at 02:29 AM
My first thought is to use Vector3.Dot(). Untested:
var dot = Vector3.Dot(windVelocity.normalized, rigidbody.velocity.normalized);
var dot = Mathf.Clamp01(dot); // Gets rid of negative values
var windSpeed = windVelocity.magnitude;
var bodyWindSpeed = rigidbody.velocity.magnitude * dot;
var bodyWindSpeed = Mathf.Clamp(bodyWindSpeed, 0.0, windSpeed);
rigidbody.AddForce(windForce * (maxWindSpeed - bodyWindSpeed) / windSpeed);
'windVelocity' is a vector representing both the direction and magnitude of the wind and needs to be defined and set elsewhere. 'bodyWindSpeed' is the speed of the object in the direction of the wind.
Your answer
![](https://koobas.hobune.stream/wayback/20220613122953im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
script to add force to the ball in a football game! 1 Answer
How can I convert velocity/direction to Force? 3 Answers
Switching between areas in a scene with triggers 0 Answers
velocity direction 1 Answer