Hi all. After completing the Mathf.Sin() For Oscillation lecture I was setting up a handful of objects and I ran into a problem. I have three objects oscillating up and down and I wanted one to start at the top, one to start in the middle, and one to start at the bottom. I was able to get the first and last easy enough (start the first object at the top with a movement vector of 0,-40,0, and start the last object at the bottom with a movement vector of 0,40,0) but I was having trouble with the middle object.
I got it working by starting the object at the top of the play space, having a movement vector of 0,-40,0, and then adding a StartingMovementFactor variable that is set to 0 for the other two objects, but is set to 0.75 for this one, and which is added to the float cycles = (Time.time / Period);
statement to become float cycles = (Time.time / Period) + StartingMovementFactor;
(see full code below), but I’m not sure if it’s the best way of doing it and I’m not sure why everything is working as it is.
For the first object I have the object start at the top of my play area with a Movement Vector of 0,-40,0. When I click play this actually moves the object immediately to the middle of the play area and starts moving down.
For the second object I have the object start at the top of my play area with a Movement Vector of 0,-40,0. When I click play this object starts at the top of the play area and starts moving down.
For the third object I have the object start at the bottom of the play area with a Movement Vector of 0,40,0. When I click play this object immediately jumps to the middle of the play area and starts moving up.
So, my actual questions:
- Why do the first and second objects immediately jump to different positions when I click Play? I noticed Rick’s does this too, I’m just having trouble wrapping my head around why.
- Is a StartingMovementFactor variable a good way to offset the starting location of the object to be part-way through its movement cycle? Is there something easier/cleaner that I could have done?
Imgur album that shows images for what I’ve described above (since the description might be confusing without them): https://imgur.com/a/ex8xNX0
Code from my Oscillator.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Oscillator : MonoBehaviour {
[SerializeField] Vector3 MovementVector;
[SerializeField] float Period = 5f;
[SerializeField] [Range(0, 1)] float StartingMovementFactor = 0f;
float MovementFactor;
Vector3 StartingPosition;
// Start is called before the first frame update
void Start() {
StartingPosition = transform.position;
}
// Update is called once per frame
void Update() {
float cycles = (Time.time / Period) + StartingMovementFactor; // Continually growing over time
const float tau = Mathf.PI * 2; // Constant value of 6.282...
float rawSinWave = Mathf.Sin(cycles * tau); // Going from -1 to 1
MovementFactor = (rawSinWave + 1f) / 2; // Recalculated to go from 0 to 1
Vector3 movementOffset = MovementVector * MovementFactor;
transform.position = StartingPosition + movementOffset;
}
}
That got a bit long and kinda complex, so if you need to ask any questions before being able to give an answer please do so. Thanks!