1

Any tips on how to move an object back and forth sinusoidally (like a pendulum, but in a linear path) along a specified 3D vector? I've got the sinusoidal motion and the vector, but I can't figure out how to combine the two.

The following are the two pieces of code I have; the vector is specified using angles from the origin.

I'm very new to coding, so please forgive me for any mistakes in the code.

This moves the object in the sinusoidal path about the origin - this is the motion I want to achieve along the 3D vector.

float rodPositionZsin = pathLength * Mathf.Sin(Time.time) + position;
transform.position = new Vector3(0, 0, rodPositionZsin);

This will move the object along the vector in the X and Y dimensions, but I'm stumped for what to do in the Z.

float Xangle = 20;
float Yangle = 50;
float Zangle = 30;

//Position Transformations
float rodPositionZsin = pathLength * Mathf.Sin(Time.time) + position;
float rodPositionY = Mathf.Cos(Yangle*Mathf.PI/180)*pathLength;
float rodPositionX = Mathf.Sin(Xangle * Mathf.PI / 180)*pathLength;
float rodPositionZ = Mathf.Tan(Zangle * Mathf.PI / 180) * pathLength;
transform.position = Vector2.MoveTowards(transform.position, new Vector2(rodPositionX, rodPositionY), pathLength * Mathf.Sin(Time.time));
rodPositionX = transform.position.x;
rodPositionY = transform.position.y;
rodPositionZ = rodPositionZsin + transform.position.z;
transform.position = new Vector3(rodPositionX, rodPositionY, rodPositionZsin);
Alex Myers
  • 6,196
  • 7
  • 23
  • 39

1 Answers1

0

if you have a vector, you just need to scale it by a sine curve, then set the object's position to that scaled vector.

so in (untested) pseudocode:

Vector3 scaledVector = originalVector* Mathf.Sin(Time.time);
youGameObject.transform.position = scaledVector

You can then add phase, frequency, and amplitude terms in your sine function to change the frequency of oscillation, how far along that vector, and the start position of the oscillation if you want to customize it further.

Edit:

Here’s how to add these.

http://jwilson.coe.uga.edu/EMT668/EMT668.Folders.F97/Feller/sine/assmt1.html

a * sin(b*x +c) + offset.

Where a is amplitude (max distance travelled) B is wavelength (1/frequency of oscillation) C is phase ( starting pos) and offset is to move the whole oscillation pattern along the vector ( make it happen away from origin center)

Adam B
  • 3,662
  • 2
  • 24
  • 33
  • Thank you so much! The code definitely works. I was wondering if you could show me how to specify the start position and path length along the vector as well. – helpmeplease Sep 24 '19 at 19:35
  • `pathlengthFromCenter * originalVector.normalized * Mathf.Sin(Time.time + timeOffset);` where you can find a timeOffset that starts it in a place you want. you could calculate timeOffset by using `Mathf.Asin(offsetAmount/pathLengthFromCenter)` – Ruzihm Sep 24 '19 at 21:36
  • Edited with more info. Take a look. Also please mark as answered if I answered your q – Adam B Sep 25 '19 at 01:10
  • Thank you so much guys! – helpmeplease Sep 25 '19 at 21:18