- Home /
Question by
unity_R0Zoro · Apr 04, 2020 at 04:06 PM ·
rigidbody2dspeedrandom.rangevector2
The speed is changing every second
I made the speed a random range but it gives me a new value every second.
I want the speed to be a random number whenever the game is played.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movemint : MonoBehaviour
{
public float speed;
private Rigidbody2D rb;
void Start()
{
rb = this.GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(0, speed);
}
void Update()
{
speed = Random.Range(550.0f, 600.0f);
}
}
Comment
Best Answer
Answer by n_rusev · Apr 04, 2020 at 04:17 PM
That is a simple fix - just move
speed = Random.Range(550.0f, 600.0f);
to your start method like so:
void Start()
{
speed = Random.Range(550.0f, 600.0f);
rb = this.GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(0, speed);
}
Note: Update is called every frame, Start only once.
Your answer
Follow this Question
Related Questions
Vector2 with increasing increment; 0 Answers
I need to add the speed of my player to the one of my projectile (c#,2d) 0 Answers
How do I make a character Lunge? 2 Answers
Keeping momentum with rigid bodies. 0 Answers
Rigidbody Problems 1 Answer