Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

252
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!