- Home /
How to make an instantiated gameobject hover in place?
I've instantiated three game objects at designated positions and I want them to hover in place when they appear. I tried using Vector3 * Mathf.Cos to oscillate the object; however, while this does make the object hover, this moves the object from the selected location of instantiation to the X: 0, Y: 0, Z: 0 position. What could be causing this? I'm new to Unity so anything helps! Thanks!
Answer by AbandonedCrypt · Feb 01, 2021 at 08:19 PM
Check out this script I wrote for my item drops some time in the past:
public class ItemDropRotation : MonoBehaviour
{
[SerializeField] private float _rotationSpeed = 30.0f;
[SerializeField] private float _bopHeight = 0.03f;
[SerializeField] private float _bopSpeed = 1.0f;
void Update()
{
transform.Rotate(Vector3.up, (_rotationSpeed * Time.deltaTime));
if (transform.rotation.eulerAngles.y > 180) _rotationSpeed *= -1;
transform.Translate(new Vector3(0, Mathf.Sin(Time.time) * _bopHeight, 0)* _bopSpeed * Time.deltaTime);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613024400im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Detecting Mouse Over Using Tooltip 2 Answers
Problem with looking to target OnMouseEnter 1 Answer
How can I make my player walk like a ghost 0 Answers
Tooltip when mousing over a game object? 0 Answers
Hover button play audio 0 Answers