- Home /
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.
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
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.
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:
Sin of the camera's angle plus the sphere's offset (in degrees) will give us a value between 1 and -1.
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).
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!
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.
@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.
@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?
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;
}
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

Follow this Question
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