Add similar manual movement for Rotation

I wanted to be able to spin some platforms in place so using a similar pattern added some serialised settings for rotation.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[DisallowMultipleComponent]
public class Oscillator : MonoBehaviour
{
[SerializeField] Vector3 movementVector = new Vector3(10f, 10f, 10f);
[Range(0.001f, 1)] [SerializeField] float period = 2f;

[SerializeField] float rotationVector;


// todo remove from inspector later
[Range(0, 1)] [SerializeField] float movementFactor; // 0 for not moved, 1 for fully moved

[Range(0, 1)] [SerializeField] float rotationFactor;

Vector3 startingPos;

Quaternion startingRotation;

// Start is called before the first frame update
void Start()
{
    startingPos = transform.position;
    startingRotation = transform.rotation;

}

// Update is called once per frame
void Update()
{
    if (period <= Mathf.Epsilon) { return; } // Protect from divide by 0
    float cycles = Time.time / period; // grows continually from 0

    const float tau = Mathf.PI * 2; // about 6.28
    float rawSinWave = Mathf.Sin(cycles * tau); // goes from -1 to +1



    movementFactor = rawSinWave / 2f + 0.5f;
    Vector3 offset = movementVector * movementFactor;
    transform.position = startingPos + offset;

    float rotationOffset = rotationVector * rotationFactor;
    transform.rotation = startingRotation * Quaternion.Euler(0,0,rotationOffset);

}

}

1 Like

Amazing work! I applaud you for going above and beyond!

Privacy & Terms