- Home /
Create an array that chooses between four random numbers (C#)
Hello my fellow Unities! I'm making a somewhat pong clone and I'm trying to make it so that when a ball spawns it will fly off "Up right", "Up left", "Down left" and so on. The angles would be 45, 135, 225, 315. Now I have created an array and so that the ball will fly off.
But the problem is that the ball just flys off in every random direction, 90, 180 degress and so on. How can I make it like this:
Here's my code:
using UnityEngine;
using System.Collections;
public class GrowingEnemyScript : MonoBehaviour
{
//Make the enemy bounce
Rigidbody2D bouncing;
public int[] angleArray;
//Speed on the ball
float speed = 8;
Vector3 velocity;
void Start()
{
//Create 4 angles for the ball to
angleArray = new int[4];
//Array values
angleArray[0] = 45;
angleArray[1] = 135;
angleArray[2] = 225;
angleArray[3] = 315;
//transform.position = Vector3.zero;
transform.rotation = Quaternion.identity;
// Random range where the ball is supposed to fly
float angle = (Random.Range(0, angleArray.Length));
// Calculate velocity for ball
velocity = new Vector2 (Mathf.Cos (angle), Mathf.Sin(angle));
// Get the ball moving!
this.GetComponent<Rigidbody2D> ().velocity = velocity * speed;
}
}
Answer by Waka-Takaki · Apr 21, 2016 at 03:58 PM
You need to perform Random.range with an int, not a float.
int selection = Random.Range(0, angleArray.Length - 1);
int angle = angleArray[selection];
Then apply your angle to the velocity using callen's method below
The float data type here makes no difference to the problem at hand. In fact, $$anonymous$$athf.Cos requires passing a float so if angle is an int then it will be implicitly type cast to a float anyway.
Does not Random.Range give a float value if passed to float? So it would give a float between 0 and 4 which would not actually be a value that could be passed to the angleArray. The angle variable above could be cast to a float for the mathf function
:$ Oh, yeah, that's what I get for not paying close enough attention. OP isn't using angle as an index into angleArray at all.
That did it! You guys really are masters at what you do!
Answer by Dave-Carlile · Apr 21, 2016 at 03:16 PM
Cos and Sin expect an angle in radians but you're passing in degrees. You can convert from degrees to radians by multiplying degrees by Mathf.Deg2Rad.
Another option would be to just store the vector in your array directly so you don't need to do the trig.
Answer by callen · Apr 21, 2016 at 03:16 PM
This should fix it:
velocity = new Vector2 (Mathf.Cos (angle*Mathf.Deg2Rad), Mathf.Sin(angle*Mathf.Deg2Rad));
Cos and Sin use radians, but youre giving it degrees. The built-in consts Deg2Rad and Rad2Deg are there for this reason.
I think this outta do it, I tried do it but, I think there's something wrong with where I set the random.range since it still makes the ball go in direction which I have not set it to be.
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Tracking HTC Vive Controller Motion Direction? 1 Answer
Moving object along with raycast 1 Answer
How do I turn 1 objects rotation into another objects movement direction? 1 Answer
Trying to move my gameobeject in a certain direction (2D, C#) 1 Answer