- Home /
 
               Question by 
               dogi257 · Aug 31, 2019 at 10:33 PM · 
                2d2d game2d-physics  
              
 
              2D random shooting problem with velocity,
Hello,
I wrote this simple line of code which should make it so that the bullet has a random shot direction, it works but the problem is that the bullet also has a random speed... Is there any way to set it up so that the bullet has a set speed (like a variable), but it still shoots in a random direction?
Thank you for your answers :D
The code is -
bul.GetComponent().velocity = new Vector2 (Randomnum2, Randomnum) * bulletSpeed;
               Comment
              
 
               
              to have the same magnitude (1 probably) you can just randomize the angle,
 float angle = Random.Range(0f, 2*$$anonymous$$athf.PI);
 bul.GetComponent().velocity($$anonymous$$athf.Cos(angle), $$anonymous$$athf.Sin(angle));
or, in the other hand, normalize the generated vector:
 bul.GetComponent().velocity = (new Vector2 (Randomnum2, Randomnum)).normalized * bulletSpeed;
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                