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.

im still working on that. i do have peak finding. but onset detection is quite hard. but i foold my teachers that my peak finding was onset detection :P it works well but id rather have propper oneset detection but i need to find more time for that.

2 Answers

2

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.

Glad to hear about the peak finding. Is that script handy? Honestly seems ridiculous that onset beat detection is not a common script for Unity already. Should be something it does relatively easily at this maturity. Synching events to a soundtrack can enhance many types of games.

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.