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 /
avatar image
0
Question by Maenor · Feb 05, 2017 at 12:45 AM · c#transformmathfmathf.sin

How does this AND Mathf.Sin actually work?!

Hello there! I'm learning Unity and as a practice exercise after the introduction i wanted to have a SpaceCraft Exposition on my Android device, all is working fine but i really don't understand how the "levitation" of the ship works!

It's relatively simple! The idea is to have the ship Oscillate between 2 apparent points and thus giving it a "levitation" feeling.

My old version used the PingPong Function but it didn't work as intended so i went to the internet for some help and found this cool piece of code:

 float originalY;
 
     public float floatStrength = 1;
 
     void Start ()
     {
         this.originalY = this.transform.position.y;
     }
 
     void Update ()
     {
         transform.position = new Vector3(transform.position.x, originalY + ((float)Mathf.Sin(Time.time) * floatStrength), transform.position.z);
     }

Now this works amazing! Exactly what i wanted (Reducing the value on the inspector tho!) ... but i have absolutely NO idea how the Oscillation works and what is preventing the ship to go higher or lower!

What "Clamps" the ship between the 2 points?!

This honetly blew my mind when it worked xP

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Feb 05, 2017 at 12:54 AM

Uhm, it's just sine. I'm not sure what else we could say. The sine function takes an angle (in this case an angle in radians) and returns an oscillating value betwen -1 and 1 for an increasing angle. That's simply what sine does.

Since you add your sine value to your original y value you move between (y-1) and (y+1) as your original y value is constant.

Comment
Add comment · 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
0

Answer by Lahzar · Feb 05, 2017 at 01:05 AM

Mathf.Sin is a function for calculating the sine of an angle. It takes an angle (in radians) and gives a value between -1 and 1.

Sine is part of trigonometry, high school curriculum, but you don't really need to know what it is to understand this script.

Just go to the wiki page for sine, https://en.wikipedia.org/wiki/Sine#Relation_to_the_unit_circle, and look at the animation for relation to unit circle - this is how your script moves the cube. (That's a sine wave.)

Look at the animation and imagine time (Time.time) is spinning the circle. Then floatStrength is how large the circle is. It's the radius of wheel, not the diameter, though.

A floatStrength of 1 means the wheel is 2 units large, and thus would clamp the object so it moves between 1 unit up and 1 unit down, and a floatStrength of 3 would clamp it between 6 units, so 3 up and 3 down.

Thanks alot to @Bunny83 for clarifying a few things!

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 Bunny83 · Feb 05, 2017 at 01:24 AM 0
Share

Sine doesn't return "radians" but a unitless value between -1 and 1. It takes an angle in radians. Also if floatStrength is "1" it will move one unit up and one unit down (so a 2 unit period). Likewise if it's 3 you get a 6 unit period (-3 to +3).

In radians a full circle (360°) would be 2*PI. Since he uses Time.time directly as angle he gets one period about every 6.28 seconds. So after about 1.57 (PI/2 --> 90°) seconds the object would be at the highest point. After 3.14 seconds it would be back at the center (PI --> 180°) and after 4.71(PI*3/2 --> 270°) seconds it would be at the lowest point.

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

283 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Mathf.PingPong Rotate Back and Forth 2 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Why is transform.LookAt backwards for my model? 2 Answers

"Lerp" back to original position after animation. 0 Answers


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