Advertisement
Krythic

FloatingObject_Script

Jun 7th, 2022
1,057
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.42 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class FloatingObject_Script : MonoBehaviour
  6. {
  7.     /// <summary>
  8.     /// The Pitch Amplitude
  9.     /// </summary>
  10.     public float pitch;
  11.     /// <summary>
  12.     /// The Yaw Amplitude
  13.     /// </summary>
  14.     public float yaw;
  15.     /// <summary>
  16.     /// The Roll Amplitude
  17.     /// </summary>
  18.     public float roll;
  19.     /// <summary>
  20.     /// The Frequency
  21.     /// </summary>
  22.     public float frequency = 1.0f;
  23.     /// <summary>
  24.     /// The Amplitude
  25.     /// </summary>
  26.     public float amplitude = 0.5f;
  27.  
  28.  
  29.     /// <summary>
  30.     /// Private variables
  31.     /// </summary>
  32.     private Vector3 _originalPosition;
  33.     private Quaternion _originalRotation;
  34.  
  35.     void Start()
  36.     {
  37.         _originalPosition = transform.position;
  38.         _originalRotation = transform.rotation;
  39.     }
  40.  
  41.     void Update()
  42.     {
  43.         float calculatedSin = Mathf.Sin(Time.fixedTime * Mathf.PI * frequency);
  44.         float newPitch = calculatedSin * pitch;
  45.         float newYaw = calculatedSin * yaw;
  46.         float newRoll = calculatedSin * roll;
  47.         float newY = calculatedSin * amplitude + _originalPosition.y;
  48.  
  49.         // Apply the new rotation and position.
  50.         transform.rotation = Quaternion.Euler(newPitch, newYaw, newRoll) * _originalRotation;
  51.         transform.position = new Vector3(_originalPosition.x,newY,_originalPosition.z);
  52.     }
  53. }
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement