72 lines
2.4 KiB
C#
72 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Sirenix.OdinInspector;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem.Editor;
|
|
using Random = UnityEngine.Random;
|
|
|
|
public class SwayMovement : MonoBehaviour{
|
|
Vector3 originalRot;
|
|
public Vector3 swayAmount;
|
|
public Vector3 swaySpeed;
|
|
|
|
private Vector3 offset;
|
|
|
|
[ShowInInspector, SerializeField]
|
|
internal List<SwayChildren> swayChildren;
|
|
|
|
[Serializable]
|
|
internal class SwayChildren{
|
|
public GameObject rootObject;
|
|
[SerializeField]
|
|
public Vector3 originalRot;
|
|
public float delay;
|
|
public Vector3 rotMultiplier = new Vector3(1f, 1f, 1f);
|
|
private Vector3 offset;
|
|
}
|
|
|
|
void Start(){
|
|
foreach (SwayChildren swayChild in swayChildren) {
|
|
SetChildOrigins(swayChild);
|
|
}
|
|
|
|
void SetChildOrigins(SwayChildren inputChild){
|
|
inputChild.originalRot = inputChild.rootObject.transform.localEulerAngles;
|
|
|
|
}
|
|
|
|
originalRot = transform.rotation.eulerAngles;
|
|
offset = new Vector3(Random.Range(-5, 1), Random.Range(-1, 5), Random.Range(-3, 1));
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update(){
|
|
Vector3 sway = new Vector3(
|
|
originalRot.x + Mathf.Sin((Time.time + offset.x) * swaySpeed.x) * swayAmount.x,
|
|
originalRot.y + Mathf.Sin((Time.time + offset.y) * swaySpeed.y) * swayAmount.y,
|
|
originalRot.z + Mathf.Sin((Time.time + offset.z) * swaySpeed.z) * swayAmount.z
|
|
);
|
|
|
|
transform.rotation = Quaternion.Euler(sway);
|
|
|
|
foreach (SwayChildren swayChild in swayChildren) {
|
|
CheckSwayChildren(swayChild);
|
|
}
|
|
|
|
void CheckSwayChildren(SwayChildren inputChild){
|
|
Vector3 childSway = new Vector3(
|
|
inputChild.originalRot.x + Mathf.Sin((Time.time + offset.x - inputChild.delay) * swaySpeed.x) * swayAmount.x * inputChild.rotMultiplier.x,
|
|
inputChild.originalRot.y + Mathf.Sin((Time.time + offset.y - inputChild.delay) * swaySpeed.y) * swayAmount.y * inputChild.rotMultiplier.y,
|
|
inputChild.originalRot.z + Mathf.Sin((Time.time + offset.z - inputChild.delay) * swaySpeed.z) * swayAmount.z * inputChild.rotMultiplier.z
|
|
);
|
|
|
|
inputChild.rootObject.transform.rotation = Quaternion.Euler(childSway);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|