Search Unity

Question Combining RotateAround and Floating/Levitate script issue?

Discussion in 'Scripting' started by bgimmelman, Nov 19, 2022.

  1. bgimmelman

    bgimmelman

    Joined:
    Aug 1, 2020
    Posts:
    1
    Hi guys,

    I'm trying to combine two scripts together that I'd like to use to make a GameObject levitate/float as well as rotate around another GameObject.

    The first script is below which makes the object float and spin.

    Code (CSharp):
    1. // Floater v0.0.2
    2. // by Donovan Keith
    3. //
    4. // [MIT License](https://opensource.org/licenses/MIT)
    5.  
    6. using UnityEngine;
    7. using System.Collections;
    8.  
    9. // Makes objects float up & down while gently spinning.
    10. public class Floater : MonoBehaviour
    11. {
    12.     // User Inputs
    13.     public float degreesPerSecond = 15.0f;
    14.     public float amplitude = 0.5f;
    15.     public float frequency = 1f;
    16.  
    17.     // Position Storage Variables
    18.     Vector3 posOffset = new Vector3();
    19.     Vector3 tempPos = new Vector3();
    20.  
    21.     // Use this for initialization
    22.     void Start()
    23.     {
    24.         // Store the starting position & rotation of the object
    25.         posOffset = transform.position;
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.         // Spin object around Y-Axis
    32.         transform.Rotate(new Vector3(0f, Time.deltaTime * degreesPerSecond, 0f), Space.World);
    33.  
    34.         // Float up/down with a Sin()
    35.         tempPos = posOffset;
    36.         tempPos.y += Mathf.Sin(Time.fixedTime * Mathf.PI * frequency) * amplitude;
    37.  
    38.         transform.position = tempPos;
    39.  
    40.     }
    41. }
    The second script below should make an object rotate around another reference object.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. // Makes objects float up & down while gently spinning.
    6. public class RotateAroundPoint : MonoBehaviour
    7. {
    8.     // User Inputs
    9.     public float rotationSpeed;
    10.     public GameObject pivotObject;
    11.  
    12.     // Use this for initialization
    13.     void Start()
    14.     {
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.  
    21.         transform.RotateAround(pivotObject.transform.position, new Vector3(0f, 1f, 0f), rotationSpeed * Time.deltaTime);
    22.     }
    23. }
    I'm not familiar with scripting in general, and all I would like to do is be able to combine these two scripts together. I've tried simply adding the second script to the object that's supposed to orbit, but only the floater script works.

    I've also tried making an empty GameObject with the RotateAroundPoint script, and having it act as the parent, but they still don't seem to work together.

    Could someone please point me in the right direction to solve this if it's possible? I can't seem to find an existing topic that explains this.

    Thank you.