Get a value that interpolates between two floats based on where character is facing
I'm making a controller script for a paper airplane and I'm currently stuck on trying to make the plane gain speed when facing down but lose speed when facing up.
The ideal way would be having a value
that's 0 when facing right or left but drops to -1 when facing up and 1 when facing down (It doesn't really matter which is 1 or -1 just that they're distinguishable). Then I'd just multiply that value
(which is somewhere in between -1 and 1) by gain
and add that to my speed variable like this speed += value * gain
.
Here's a picture to better understand.
I already got the information where the plane is currently facing (both in vector form and degrees) but how can I do this?
Answer by SarperS · Aug 07, 2016 at 06:36 PM
Below line would give you a value between 1 and 0. 1 when the transform looks downward with 90 degrees and 0 when directly upwards.
var dir = -transform.forward.y * 0.5f + 0.5f
If you don't want to stop the plane completely when it looks upwards, instead of multiplying your movement speed value directly with this value, I would suggest using a lerp.
var dir = -transform.forward.y * 0.5f + 0.5f;
speed = Mathf.Lerp( minSpeed, maxSpeed, dir);
nice! but I think he wanted -1 result when facing up: -transform.forward.y
Yep, just get rid of the * .5f + .5f part. I thought lerping was the better way I guess. $$anonymous$$ore control.
I'm having a hard time wrapping my head around your solution. What is the dir
variable?
How I currently go around moving my plane is by rotating it with rb2D.rotation
then just move it forward with rb2D.velocity
by setting it to whatever my speed
variable is.
Does this: * 0.5f + 0.5f
give me the value
I specified in my post?
dir is just an example variable, in your case it was named "value".
"* 0.5f + 0.5f" part just maps the value from -1, 1 range to 0-1 range.
It's pretty simple actually. (-1 * 0.5 + 0.5)
gives 0, (1 * 0.5 + 0.5)
gives 1. And any value in between is mapped as such. I added that part because I thought you wanted to interpolate between two floats, Lerp method takes a value between 0 and 1 for interpolation.
So if you need -1, 1 range just use the -transform.forward.y
If you would like it to be between 0, 1 use -transform.forward.y * 0.5f + 0.5f
This math is some next level shit :P and I applaud you for understanding it. However, have you tried this yourself? Because my results for -transform.right.y * 0.5f + 0.5f
is 0,5 for left and right, 1 for down and -5.960464e-08 for up.
Oh by the way, I thought it was a 3D game. In your case you'll have to use -transform.right.y
since X is the axis you are rotating around.
This is really close to what I need but not quite. When I go right I get 0
(Which is what I want) but when I turn left I get 7.897299E-09
ins$$anonymous$$d of 0. Also, When I'm facing 45° I get 0.7
something, I need to get 0.5 ins$$anonymous$$d to get the right results. Do you have any other ideas?
Your answer
Follow this Question
Related Questions
The object is incorrectly flipped. 0 Answers
Mesh looks like overlapping. 0 Answers
Mathf.Lerp for integers 1 Answer
Moving an object to a direction in degrees at a specific speed? 0 Answers