Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by PlayCreatively · Aug 07, 2016 at 01:23 PM · 2dinterpolationdegreesinterpolate

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?

alt text

untitled-2.png (15.5 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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);

Comment
Add comment · Show 13 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Glurth · Aug 07, 2016 at 06:43 PM 0
Share

nice! but I think he wanted -1 result when facing up: -transform.forward.y

avatar image SarperS Glurth · Aug 07, 2016 at 06:47 PM 0
Share

Yep, just get rid of the * .5f + .5f part. I thought lerping was the better way I guess. $$anonymous$$ore control.

avatar image Glurth SarperS · Aug 07, 2016 at 06:56 PM 0
Share

oh, didn't see that part initially: agreed.

avatar image PlayCreatively · Aug 07, 2016 at 07:00 PM 0
Share

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.rotationthen 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?

avatar image SarperS PlayCreatively · Aug 07, 2016 at 07:34 PM 0
Share

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

avatar image PlayCreatively SarperS · Aug 08, 2016 at 01:12 PM 0
Share

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.

Show more comments
avatar image SarperS PlayCreatively · Aug 07, 2016 at 07:43 PM 0
Share

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.

avatar image PlayCreatively SarperS · Aug 07, 2016 at 11:44 PM 0
Share

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-09ins$$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?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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

converting degrees per second into speed 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges