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
2
Question by sona.viswam · Apr 09, 2013 at 09:23 AM · transformpositionunitsine-wave

How to make a sine wave with a transform

Transform should move as a wave.

How to do it in c#?

alt text

wave.png (11.1 kB)
Comment
Add comment · Show 6
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 Chronos-L · Apr 09, 2013 at 10:03 AM 0
Share

@sonaviswam, you get downvotes because you asked a bad question ( in this case, I can see that you have done nothing at all in your effort to solve the problem).

avatar image Racoon022 Chronos-L · Jun 23, 2016 at 09:37 PM 0
Share

Ironically, this is now the top Google result for sine waves in unity, and everyone can see you have done nothing to solve this problem.

avatar image sona.viswam · Apr 09, 2013 at 10:05 AM 0
Share
 void Update () {

     firstPos = floorPos + new Vector3(0f, 0.8f, 0f);
     lastPos = floorPos  + new Vector3(0f, 0f, 0f);            
     lerp = $$anonymous$$athf.PingPong(Time.time, duration) / duration;        
     transform.position = Vector3.Lerp(firstPos, lastPos, lerp);        

 }

These much i tried. It wont move x positions. i need that also

avatar image sona.viswam · Apr 09, 2013 at 10:20 AM 0
Share

my charcter is flying in sky. obstacle should move as above image from left to right and then right to left contiously with changing y position also. within limit point. until charcter escape from that. i searched in google so much. But not getting answer.

avatar image Loius · Apr 09, 2013 at 11:07 AM -1
Share

you're not getting a good solution because you're at the point where you have all the information you need to solve your specific problem on your own. you need to now apply logic to the information you've been given.

the sin function produces movement between + and - one. multiply it by the width of your zone, add the center of your zone, and you're done.

transform.position = Vector3( center.x + $$anonymous$$athf.Sin(Time.time) * size.x, ...y..., ...z...);

avatar image sona.viswam · Apr 09, 2013 at 12:05 PM 0
Share

basic things like value sinf got it from unity site. but logic didnt get . being new to unity it is difficult for me

2 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

Answer by fafase · Apr 09, 2013 at 09:31 AM

Use Mathf.Sin. Ok you want the guy to go left and right AND up and down. Now it looks like the guy is somehow "bouncing" at the bottom instead of having a rounded turn.

So!!! You need to use a cos function on the y axis but as absolute value to create the sharp shape at the bottom. Then you use another cos function on the x axis so that it moves left and right. But first, create an empty game object that you place where you want this to happen. Then add your moving object as child to it.

Then use the code below on the child object.

 float amplitudeX = 10.0f;
 float amplitudeY = 5.0f;
 float omegaX = 1.0f;
 float omegaY = 5.0f;
 float index;

 public void Update(){
     index += Time.deltaTime;
     float x = amplitudeX*Mathf.Cos (omegaX*index);
     float y = Mathf.Abs (amplitudeY*Mathf.Sin (omegaY*index));
     transform.localPosition= new Vector3(x,y,0);
 }

Now your object is going left to right to left and so on, but also up and down with the bouncing effect at the bottom.

Now it is up to you to modify amplitude to define how large is the movement and omega defines how fast is the movement. If you want to remove the bouncing, remove the Mathf.Abs.

Comment
Add comment · Show 3 · 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 Chronos-L · Apr 09, 2013 at 10:06 AM 0
Share

it is a simple Vector3 to move the gameObject sideway so that it wouldn't looked like it is jumping up and down and more like following a sine wave path.

avatar image sona.viswam · Apr 09, 2013 at 10:13 AM 0
Share

@fafase:Thanks. Its working fine.

avatar image JackofTraes · Mar 08, 2017 at 06:00 PM 0
Share

If anyone is looking for a normal Sine wave solution for making an object float up and down, this might help a little:

     void SmoothSineWave () {
         // y(t) = A * sin(ωt + θ) [Basic Sine Wave Equation]
         // [A = amplitude | ω = AngularFrequency ((2*PI)f) | f = 1/T | T = [period (s)] | θ = phase | t = elapsedTime]
         // Public/Serialized Variables: amplitude, period, phase
         // Private/Non-serialized Variables: frequency, angularFrequency, elapsedTime
         // Local Variables: omegaProduct, y
 
         // If the value of period has altered last known frequency...
         if (1 / (period) != frequency) {
             // Recalculate frequency & omega.
             frequency = 1 / (period);
             angularFrequency = (2 * $$anonymous$$athf.PI) * frequency;
         }
         // Update elapsed time.
         elapsedTime += Time.deltaTime;
         // Calculate new omega-time product.
         float omegaProduct = (angularFrequency * elapsedTime);
         // Plug in all calculated variables into the complete Sine wave equation.
         float y = (amplitude * $$anonymous$$athf.Sin (omegaProduct + phase));
         // 
         transform.localPosition = new Vector3 (0, y, 0);
     }

avatar image
1

Answer by iMagesBlues · Jun 24, 2016 at 05:06 AM

A no code-solution is to use Mecanim. Draw a sine-wave graph for the transform's y-position variable in the Animation Window > Curves.

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

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

16 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

Related Questions

a child object that inherits only position from parent 2 Answers

How do I make the crosshair go closer/get bigger if an item/wall is close to you? 2 Answers

What is wrong with my zipline script? 2 Answers

Why does this placement script not work? 1 Answer

What's wrong with this simple position script? 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