Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by davidt · Jan 09, 2012 at 09:41 AM · movementjumpingtrigonometrymaths

Lerp & jumping with sin/cos

I've just started messing around with moving cubes on a plane in response to keyboard input. I'm trying to keep things simple with no physics, movement of 1 metre/unit at a time etc. Think Frogger ;)

In Update(), I'm calling GetKeyDown and if an arrow key is pressed, calculating a new Vector3 for the target position and then calling Lerp.

 transform.position = Vector3.Lerp (transform.position, target, (Time.time - startTime) / duration);

This works well and my cube slides in the desired direction.

I now want to make the cube "hop" in the air by applying a sin offset to my transform.position.y, but just can't figure it out. I think I need to track my movement lerp value in either X (for left/right movement) or Z (for north/south movement). From that, I somehow need to multiply by sin or cos to get the Y offset, then apply it to the transform.

Any thoughts or two-line code samples appreciated. I do intend to look at CharacterController tonight btw.

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
2
Best Answer

Answer by Owen-Reynolds · Jan 09, 2012 at 05:30 PM

In real trig (the kind sin/cos uses) 0 degrees faces right, goes counter-clockwise, using radians (PI radians is 180 degrees.) Sin is the y-coord, and cos is the x-coord, both between from -1 and 1. So, as you rotate from 0 to PI radians, sin goes from 0 to 1 to 0. This is regular high-school math, or see Wikipedia. It's a lot easier to tweak if you can visualize it.

The standard way to hop with sin (yes, real hops do make a sin wave) is: y=HT*sin(A) where A goes from 0 to PI. Since you already have a Lerp value going from 0 to 1, you just need to scale it:

 y = jumpHT * Mathf.Sin( Mathf.PI * ((Time.time - startTime)/duration) );
                                    ^^^^^ your old lerp value    ^^^^^
Comment
Add comment · Show 1 · 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 davidt · Jan 09, 2012 at 09:18 PM 0
Share

Thank you Owen. That's almost exactly what I worked out on pen and paper about 2 hours after I posted. As it's 30 years since high school, I went back to basics for some revision :)

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 name 'Joystick' does not denote a valid type ('not found') 2 Answers

A better way to do jumping? 2 Answers

Player movement 0 Answers

Controlling direction while jumping using CharacterController 1 Answer

TERA style movement 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