Hello
I start with unity and the c # and in 2D
After several research I can not do what I want.
I throw a ball up, but it always goes in the same direction, and I want it to go in random direction
I will give you the code if need more information.
Desolate if it’s badly translated because I’m French
Thank you in advance
Ok so i am guessing you want to randomize 2d z rotation. Google translated:
Ok, je suppose que vous voulez randomiser la rotation 2d z. (Google traduit)
transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y, transform.rotation.z + Random.Range(-180, 180));
j
I just try to integrate the code but it does not change anything.
I thought it was more transform.position with a vector2.
but impossible to find.
void Start ()
{
rb2D = GetComponent<Rigidbody2D>(); //Connect rb2D with the Ball Rigidbody 2D Component
audioSource = GetComponent<AudioSource>(); //Connect rb2D with the Ball Audio Source Component
}
// Update is called once per frame
void Update ()
{
//If The player pressed Space button & The ball is not inPlay (in moving) & The game can start
if (Input.GetButtonDown("Jump") && ballInPlay == false && GameManager_script.startGame)
{
transform.parent = null; //Disconnect the Ball from the Paddle
ballInPlay = true; //The Ball is inPlay (is moving)
rb2D.isKinematic = false; //Uncheck the isKinematic in the Rigidbody 2D
rb2D.AddForce (new Vector3(vitesse, y: vitesse, z: 0)); //Adding force to the Balle
}
add it as a new line
void Start ()
{
rb2D = GetComponent<Rigidbody2D>(); //Connect rb2D with the Ball Rigidbody 2D Component
audioSource = GetComponent<AudioSource>(); //Connect rb2D with the Ball Audio Source Component
}
// Update is called once per frame
void Update ()
{
//If The player pressed Space button & The ball is not inPlay (in moving) & The game can start
if (Input.GetButtonDown("Jump") && ballInPlay == false && GameManager_script.startGame)
{
transform.parent = null; //Disconnect the Ball from the Paddle
ballInPlay = true; //The Ball is inPlay (is moving)
rb2D.isKinematic = false; //Uncheck the isKinematic in the Rigidbody 2D
transform.rotation = Quaternion.Euler(transform.rotation.x, transform.rotation.y, transform.rotation.z + Random.Range(-180, 180));
rb2D.AddForce (new Vector3(vitesse, y: vitesse, z: 0)); //Adding force to the Balle
}
try keeping t in that order because transform.parent may mess it up