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?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
float radius;
public Rigidbody2D rb;
public float ballForce;
private Vector2 RandomVector(float min, float max)
{
var x = Random.Range(min, max);
var y = Random.Range(min, max);
return new Vector2(x, y);
}
// Start is called before the first frame update
void Start()
{
radius = transform.localScale.x / 2;
var rb = GetComponent<Rigidbody2D>();
rb.velocity = RandomVector(-5f, 5f);
ballForce = 100f;
rb.AddForce(new Vector2(ballForce, ballForce));
}
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.
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.