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 morius · Sep 29, 2015 at 03:17 AM · rotationsine-wavesine

Sine movement in rotated object?

Hello everyone. Basically, I want to do something like this: I know it's already been asked, specifically in this question: http://answers.unity3d.com/questions/797082/sine-movement-at-any-angle.html

But the answer (which is "use an animation") is not what I want to do. I want to do this using Mathematics.

I can easily move an object in a normal sine wave, and a quick "hack" for me to move it in a rotated sine wave was to put the object inside another parent object which was already rotated and then just move its localPosition.

However, I don't want to do that.

Basically, I have the angle of rotation (and the aiming vector), I'm assuming there's got to be a way to modify the normal formula for moving the position along a sine wave based on one of these two parameters?

If someone can help me out, I'd really appreciate it!

Comment
Add comment · Show 2
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 supernat · Sep 29, 2015 at 03:32 AM 0
Share

I'm on my phone, can't type out a full solution, but the straight forward approach is to use the sin(time*rate) which will produce a value between -1 to +1 over time. Just translate your object over the Y axis by this value (add in a multiplier to make it move higher and lower). Translate the X value by a constant speed, like time.deltaTime*speed. Translate along the forward vector.

avatar image morius supernat · Sep 29, 2015 at 03:37 AM 0
Share

Hi, What I don't understand is the very last part. I don't know which operation to do with both vectors (the one that I created based on the sine value and the forward vector).

1 Reply

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

Answer by Eno-Khaon · Sep 29, 2015 at 04:05 AM

Hmm... this shouldn't be too bad, really. Have the projectiles take note of where they're created and their intended direction, then your sine wave can come from the distance the projectile has traveled in that direction, or the time it's existed.

For a quick idea:

 // C#
 
 // Assuming this is a 2D presentation, like the example gif file...
 public Vector2 startPosition;
 public Vector2 direction;
 public float speed;
 
 private float time; // and/or direction
 private Vector2 crossDirection; // More important for a 3D approach, but still a good point of reference
 
 void Start()
 {
     time = 0.0f;
     direction = direction.normalized;
     crossDirection = new Vector2(direction.y, -direction.x); // A quick right angle for 2D
     startPosition = transform.position;
 }
 
 void Update()
 {
     transform.position = startPosition + (direction * speed * time) + (crossDirection * Mathf.Sin(time));
     time += Time.deltaTime;
 }

That should cover pretty much all the basic concepts. There are many ways to handle it, but the most uniform approaches need the most structure for consistent results.

Edit: Most importantly, I suppose, the key element is your defined direction and it's crossing direction. Separate those elements out and you can have your distance traveled on a straight line while the perpendicular one defines your sine wave.

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 morius · Sep 29, 2015 at 04:25 AM 0
Share

Thank you very much! This is what I needed.

Just one more thing (sorry to bother you). Where would I add a sine wave amplitude in this formula? What about wave frequency?

I'm assu$$anonymous$$g it's in the section:

 (crossDirection*$$anonymous$$athf.Sin(time))

But I may be wrong

avatar image Eno-Khaon · Sep 29, 2015 at 06:08 AM 1
Share

Yep. I kinda threw in a fairly bare $$anonymous$$imum, but you can modify the amplitude of the sine waves and their frequency like this:

 ... (crossDirection * $$anonymous$$athf.Sin(time * frequency) * amplitude)

CrossDirection would be a normalized vector by this implementation, so the width would effectively be doubled, since sine waves range from -1 to 1 (i.e. a range of 2). If you want the width to be literal, you can just halve it in the formula or halve crossDirection at the start ins$$anonymous$$d, if it wouldn't do any harm elsewhere.

avatar image morius Eno-Khaon · Sep 29, 2015 at 05:33 PM 0
Share

Thanks a lot for the information and for the help!

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

31 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

Related Questions

Flip over an object (smooth transition) 3 Answers

How to rotate camera on sine wave. 0 Answers

Create a sine wave(using line renderer) and reflect it from colliders 0 Answers

How do you calculate a transform based on Sine wave 3 Answers

Code to rotate around a local axis until local Y is 0? 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