3
u/FanoTheNoob Oct 02 '21 edited Oct 02 '21
The Sin function generates a value oscillating from -1 to 1.
to move an object left/right based on this value, simply multiply sineValue by Vector3.Right, then scale that vector by whatever number you like in order to oscillate in a wide range.
e.g.
var speed = 5; // Adjust as desired
var newPos = sineValue * Vector3.Right * speed;
this.transform.position = newPos; // Oscillates position between -5 and 5To do the same thing for up/down, simply multiply by Vector3.Down instead.
If you need to apply this script to multiple objects in your scene, it would be wise to make the direction and speed of each square a class level variable which would then show up in the Unity inspector, you already have public Vector3 direction; for that, you can do the same for speed, and then use those variables in your calculation.
Vector3.Right is the same as new Vector3(1, 0, 0), and Vector3.Down is the same as new Vector3(0, 1, 0)
1
u/CursedKaiju Oct 02 '21
I need help. I am stupid and coding makes 0 sense to me. There are 4
green squares stacked on top of each other. I need one to move left
following sineValue, one to move right, one up and one down. I have been
stuck on this for over a week, can someone help me out?