Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

258
Vistas
How to put random speed on the position of the y-axis only?

Sorry, I'm a beginner in Unity but I want to move a gameobject on the Y-axis only and have it move with random speed so that sometimes it moves fast and sometimes slower.

this is my code. (it jitters because i figured out my mistake is I put the random speed on the update but I don't know how to fix it)

public class RandomUpDown : MonoBehaviour
{
     [SerializeField] float minSpeed = 1f;

     [SerializeField] float maxSpeed = 2f;
     
     [SerializeField] float height = 4f;

     Vector3 pos;

     private void Start()
     {
         pos = transform.position;
     }

     void Update()
     {
        float randomSpeed = Random.Range(minSpeed, maxSpeed);
        float newY = Mathf.Sin(Time.time * randomSpeed) * height + pos.y;  
        transform.position = new Vector3(transform.position.x, newY, transform.position.z);
     } 
} 
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

it really depends on what you want, but you can do this with a coroutine and a loop.

for example:

void Start()
{
    StartCoroutine(myRandLoop)
}

public IEnumerator myRandLoop()
{
    while(true)
    {
        float randomSpeed = Random.Range(minSpeed, maxSpeed);
        float newY = Mathf.Sin(Time.time * randomSpeed) * height + pos.y;  
        transform.position = new Vector3(transform.position.x, newY, transform.position.z);
        yield return new WaitForSeconds(delayAmount)
    }
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda