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 hedgeh0g · Nov 09, 2017 at 10:39 AM · scripting problemmathsine-wave

How to get the "Perfect" Sine Wave Pattern

Hello folks,

I already managed to move a gameobject in a sine wave pattern thanks to this answer: https://answers.unity.com/questions/803434/how-to-make-projectile-to-shoot-in-a-sine-wave-pat.html

In my 2D game the level designer is able to place enemies by inputting on the Inspector their start_pos and their end_pos. The enemy will then calculate the direction and move towards. My problem is: I can't figure out how to automatically get the axis in which the sin wave will move depending on the direction.

Example: if my direction is (1.0, 0.0), the axis must be (0.0, 1.0), but if the direction is (0.0, 1.0), the axis must be (1.0, 0.0). And I can't figure out how to make this calculation automatic. So my level designer doesn't need to care about it. Do you have any possible suggestion other than manually input the axis in the Inspector?

Thanks in advance.

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

Answer by Daemonhahn · Nov 09, 2017 at 12:25 PM

Sounds like you need to learn about vector maths?

You can work out a directional vector by doing:

Initial Position - Target Position.

You can also then get the square magnitude of that vector to find its length, or "distance". (not useful here but useful for other things).

So try:

Vector3 direction = transform.position - target.position;

then normalize this to turn it into a direction (unit vector)

direction.Normalize();

you can then use this to work out what direction they are initially facing as they start moving etc.

Then you can flip this direction by multiplying it negatively like this : direction = -direction; So once you work out the direction, you can then use it to affect your wave, simple!

Comment
Add comment · Show 2 · 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 Daemonhahn · Nov 09, 2017 at 12:25 PM 0
Share

Also if the direction is the wrong way around, reverse the Initial Position - Target Position. part of the equation.

Please mark my answer as correct if I have helped you learn how to solve this, or how to perform direction based vector math equations

avatar image hedgeh0g · Nov 10, 2017 at 08:27 AM 0
Share

I already got at that point, I'm marking it as an answer but I will add this:

The solution was more obvious than expected: just rotate the direction vector by 90 degrees and you always get the correct axis on which the wave should... wave.

 mid = end - start;
                 mid = mid.normalized;
                 dir = new Vector3 (mid.x, mid.y, 0);
                 axis = Quaternion.Euler(0.0f, 0.0f, 90.0f) * dir;

Cheers!

avatar image
0

Answer by Hellium · Nov 09, 2017 at 10:56 AM

 Vector3 direction = ... ;
 Vector3 axis = Vector3.Cross( direction, Vector3.up )

Or maybe try this, depending on your needs :

 Vector3 direction = ... ;
 Vector3 axis = Vector3.Cross( direction.x < 0 ? -direction : direction, Vector3.up )
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

128 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

Related Questions

What's with this absurd index array input? 1 Answer

Changing the Play Rate (Frequency) of a sine wave in real-time 1 Answer

(Scripting) Getting a circle of points in an array 1 Answer

How do I get this code to run specified amount of times 1 Answer

Creating an UI GPS map 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