Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to get object to continually float up

Discussion in 'Scripting' started by SuperCrow2, Oct 18, 2020.

  1. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    This script gets it to float up and down, but I don't want it to go back down, how do I get it so its always moving up?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FloatingObject : MonoBehaviour
    6.  
    7.  
    8. {
    9. // Position Storage Variables
    10. Vector3 posOffset = new Vector3();
    11. Vector3 tempPos = new Vector3();
    12. public float amplitude = 0.5f;
    13. public float frequency = 1f;
    14. public float degreesPerSecond = 15.0f; //to rotate the object on Y axis
    15.  
    16.     void Start()
    17. {
    18.     // Store the starting position & rotation of the object
    19.     posOffset = transform.position;
    20.  
    21. }
    22.  
    23.  
    24.     void Update()
    25.     {
    26.  
    27.         // Spin object around Y-Axis
    28.         transform.Rotate(new Vector3(0f, Time.deltaTime * degreesPerSecond, 0f), Space.World);
    29.  
    30.         //float up and down
    31.         tempPos = posOffset;
    32.         tempPos.y += Mathf.Sin(Time.fixedTime * Mathf.PI * frequency) * amplitude;
    33.  
    34.         transform.position = tempPos;
    35.     }
    36. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
  3. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    You've just posted a screenshot of the documentation page, so I'm not sure what you're talking about.
     
  5. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584

    I had to comment out the two public void statements at the very top there so I could test out other aspects of my game. "Translate" in those two public void statements has a red underline

    Code (CSharp):
    1.    
    2.  
    3.   //public void Translate(Vector3 translation);
    4.     //public void Translate(Vector3 translation, Space relativeTo = Space.Self);
    5.  
    6.  
    7.  
    8.  
    9.     void Start()
    10.     {
    11.         yourText.enabled = false; // You may need to use .SetActive(false)
    12.  
    13.         // Store the starting position & rotation of the object
    14.           posOffset = transform.position;
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         // Move the object forward along its z axis 1 unit/second.
    21.        transform.Translate(Vector3.forward * Time.deltaTime);
    22.  
    23.         // Move the object upward in world space 1 unit/second.
    24.         transform.Translate(Vector3.up * Time.deltaTime, Space.World);
    25.     }
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    Yeah you're not supposed to copy that part into your script. It's just explaining the signature of the method. The way to actually use it is the way from the example code which you have in your Update method here. Comment out line 21 too and you're good to go.
     
  7. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    I have a text object that appears when you get to a certain location and in the OP it prevented it from being glitchy. The script you showed me glitched it out where if I enter a different area, then the text appears, while it doesn't appear where it's supposed to. So its supposed to appear in point A but it doesn't, but it does appear at point B.

    Going back into it causes it to somehow respawn.

    The script in the OP fixes all of those issues except it floats up and down.

    Im gonna need another fix for it if I were to use the script you showed me.
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    The script I suggested simply moves an object, which is all you originally asked for.

    You're going to need to provide a lot more context about your project if you want to solve this other issue. I will say that both scripts simply set the position of this object. Any other interaction with other objects happens in some other script.
     
  9. NullOverrideX

    NullOverrideX

    Joined:
    Oct 18, 2020
    Posts:
    15
    Maybe shoot a raycast downward and detect the distance. If the distance is too low, raise the height. If it's too high, apply gravity.

    Also could still use your sine function afterwards to make it seem floating.

    If you are asking why I sent so late, that's because I'm in Hong Kong. I'm nine years old, and not quite experienced, so if I'm wrong, please don't judge me