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 AlexJBoyd · Mar 04, 2014 at 10:14 PM · transformsine-wavesine

How do you calculate a transform based on Sine wave

Sorry if this is not worded correctly, but I will do my best to explain. What I am trying to achieve is to change multiple objects y positions based on the rotation of the camera.

To illustrate the idea, there is a circle of spheres. As the camera rotates around the spheres, I want the closest sphere to the camera's y position to be at 0 and the one opposite of it to be at our max (in this case 3). Then the other spheres around the circle to be at the appropriate heights ranging from 0 to 3.

alt text

The code I have so far looks like this:

 omegaY = CameraController.GetCamY();
 
 float y = Mathf.Abs(ampY*Mathf.Sin((omegaY + offset)));
 
 transform.position = new Vector3(transform.position.x,y,transform.position.z);


omegaY: roatation of the camera / ampY: the max altitude I want the sphere to be / offset: what i think to differentiate the orbs from one another

I know this is alot of info, and maybe complex (or not I reall don't know). But I hope someone who has a much better understanding of math will be able to help.

Thanks

capture.jpg (49.8 kB)
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

3 Replies

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

Answer by AlexJBoyd · Mar 05, 2014 at 03:04 PM

@robertbu I semi solved my own problem. The real reason it was not working properly was due to the fact I was getting transform.rotation and not the euler angle of my camera. This was resulting in getting a number between -1 and 1, therefore hardly moving the orbs. Thank you again.

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 Araph · Mar 04, 2014 at 10:50 PM

I think you could use something like this:

 var targetCamera : Transform;
 var offset : float;
 var magnitude : float = 1.5;
 
 function Update () {
     var angle : float = targetCamera.rotation.eulerAngles.y;
     
     transform.position.y = (Mathf.Sin(angle + offset)
                            * magnitude)
                            + magnitude;
 }

The math looks a little weird, but here's the logic of it:

  1. Sin of the camera's angle plus the sphere's offset (in degrees) will give us a value between 1 and -1.

  2. We can multiply this by a preset magnitude (default of 1.5) to get a value between -1.5 and 1.5 (in other words, a range of 3).

  3. We can add the magnitude to this value to make sure it's always positive, which gives us the final result: a value between 0 and 3, determined by the camera's angle.

Just apply this script to each sphere and drag the player's camera into the targetCamera variable, then change the offset to a multiple of 90 (if you have four spheres). Closest sphere should have an offset of 0, second should be 90, and so on around the circle.

Sorry if I'm restating things you already understood; I'd just prefer to make sure any jumps of logic I make are actually useful instead of potentially coming out of the blue. I hope this helps!

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 AlexJBoyd · Mar 04, 2014 at 11:14 PM 0
Share

Thank you for the response, I will unfortunately have to wait until tomorrow to test this. However, if it does work I will come back and let you know.

avatar image robertbu · Mar 05, 2014 at 12:01 AM 0
Share

@Araph - approach looks right, but there are a couple of tweaks you need. $$anonymous$$athf.Sin() takes radians not degrees. While you don't initialize it, offset will also have to be in radians as well. You don't include a base height for your 'y'...you could get it from the initial position of the object. @$$anonymous$$JBoyd - magnitude needs to be set to 1/2 of your ampY or you need to redo the calculation a bit.

avatar image AlexJBoyd · Mar 05, 2014 at 01:11 AM 0
Share

@robertbu Well I assumed I would need to redo my calculations, however my math skills are lacking. Could you maybe guide a little more what you mean?

avatar image
0

Answer by robertbu · Mar 05, 2014 at 01:30 AM

Here is how I might approach the problem. Your object should be placed at its bottom-most location, even if that is not where you want it to start. This will be the base position of the object and it will be allowed to rise 'ampY' above that position. 'offset' is in Radians so Mathf.PI is the same as 180 degrees. If you don't supply a camera, it uses Camera.main.

 #pragma strict
 
 var camTrans : Transform;
 var offset = Mathf.PI;
 var ampY = 1.5;

 private var basePos : Vector3;
 
 function Start() {
     basePos = transform.position;
     if (camTrans == null) 
          camTrans = Camera.main.transform;
 }
  
 function Update () {
     var angle = camTrans.eulerAngles.y;
     var delta = (Mathf.Sin(Mathf.Deg2Rad * angle + offset) + 1.0) / 2.0 * ampY;
     transform.position = basePos + Vector3.up * delta;
 }
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 AlexJBoyd · Mar 05, 2014 at 01:37 AM 0
Share

Well looking over your solution, it makes more sense what I need to do. I currently don't have access to the project (server restrictions), but after I test it I will let you know. Thank you for the time.

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

21 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

Related Questions

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

How to make a sine wave with a transform 2 Answers

Sine movement in rotated object? 1 Answer

Weapon Bullet Question - Steadily increasing sine wave 1 Answer

using MoveObject to perform 2 translations at the same time 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