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

Need ball to move at random angle but not random speed

Discussion in 'Scripting' started by UnderworldGames, Nov 20, 2020.

  1. UnderworldGames

    UnderworldGames

    Joined:
    Oct 26, 2020
    Posts:
    11
    My ball needs to begin the scene moving at a random angle, but not a random speed. Only way I've found online to move it at a random angle is with rb.velocity = RandomVector. This successfully moves it at a random angle but also changes the speed of the ball, which I don't want. How can I do this?

    Related: I am using Rigidbody force to control the speed of the ball. Should I do this? Or should I have force and speed as separate, so that I can increase speed but leave force the same?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Ball : MonoBehaviour
    6. {
    7.     float radius;
    8.     public Rigidbody2D rb;
    9.     public float ballForce;
    10.     private Vector2 RandomVector(float min, float max)
    11.     {
    12.         var x = Random.Range(min, max);
    13.         var y = Random.Range(min, max);
    14.         return new Vector2(x, y);
    15.     }
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         radius = transform.localScale.x / 2;
    21.         var rb = GetComponent<Rigidbody2D>();
    22.         rb.velocity = RandomVector(-5f, 5f);
    23.         ballForce = 100f;
    24.         rb.AddForce(new Vector2(ballForce, ballForce));
    25.     }
     
  2. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    You can generate a vector from a random angle (in radians) using Mathf's trig functions, and then multiply that by your desired speed:
    Code (csharp):
    1. float angle = Random.Range(0f, 2f * Mathf.PI);
    2. Vector2 randomDir = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle));
    3. return randomDir * speed;
     
    Last edited: Nov 20, 2020
    Kurt-Dekker likes this.
  3. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    You can also use
    Random.insideUnitCircle
    and normalize the result:

    Code (CSharp):
    1. rb.velocity = Random.insideUnitCircle.normalized;
     
  4. UnderworldGames

    UnderworldGames

    Joined:
    Oct 26, 2020
    Posts:
    11
    Where do I put those lines? I assume the first two are variables and go at the top but I've never used return, I don't know what that is.
     
  5. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    But the code you posted has that? And this goes into the same place. You do need to change your parameters from min/max to one "speed" and change line 22 to match.
     
  6. UnderworldGames

    UnderworldGames

    Joined:
    Oct 26, 2020
    Posts:
    11
    Oh that's funny I copied that line of code from somewhere so that just goes to show you that I have no idea what I'm doing.

    Also, I entered all three lines of code that you suggested and every one of them produced an error in my console.
     
  7. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    It sounds like you should seek out a beginner C# tutorial to teach you the ropes. Showing you how to do something won't get you very far if you don't know how to use that information.
     
    PraetorBlue likes this.
  8. Ray_Sovranti

    Ray_Sovranti

    Joined:
    Oct 28, 2020
    Posts:
    172
    Can't help fix the problem if you don't say what the errors are or if you don't post your new code after pasting in the lines.