- Home /
The question is answered, right answer was accepted
I want to set a random rotation with Random.Range, but its adding constantly [2D]
Working on a 2D Top Down Shooter, in my Shoot method im ejecting casings from the bullet from an ejectPoint (basically an empty gameobject). To make it look a little bit better i wanted to rotate the ejectpoint so they dont get ejected in a straight line and it kind of works. The problem is, it doesnt set the rotation but rather keeps adding to the rotation.
void Shoot()
{
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
GameObject casing = Instantiate(casingPrefab, ejectPoint.position, Quaternion.Euler(0, 0, Random.Range(0.0f, 360.0f)));
Rigidbody2D bulletrb = bullet.GetComponent<Rigidbody2D>();
Rigidbody2D casingrb = casing.GetComponent<Rigidbody2D>();
//add rotation for spread??
float randomZ = Random.Range(10,20);
ejectPoint.transform.Rotate(0, 0, randomZ);
bulletrb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
casingrb.AddForce(ejectPoint.up * ejectForce, ForceMode2D.Impulse);
}
Answer by Klarzahs · Oct 06, 2020 at 03:59 PM
Hi @Lord_Scio,
as you have recognized Transform.Rotate()
adds to the current rotation. To actually rotate TO a certain angle, why not just set the euler angles, like this:
ejectPoint.transform.eulerAngles = new Vector3(0, 0, randomZ);
Hey @Klarzahs,
that works just how i wanted, thanks! Now i just need to adjust the values ^^
Follow this Question
Related Questions
Rotate Sprite along shape? 0 Answers
How to control a 2d character in an top-down android game with the standard joystick? 1 Answer
How to smoothly rotate object in direction of analog joystick? 1 Answer
Randomize shape.rotation for a particle emitter shape, sub emitter instance 1 Answer
Creating a homing missile on 2D, problem with transform.lookat() 1 Answer