- Home /
Gun Random Rotation
Hi i'm making a game with Unity (https://www.youtube.com/channel/UCWrC78PXS3Frrp1Sk9TSp7A go here to see the game) and i want to simulate gun inaccuracy by randomly rotating it. The problem is that it save the world space rotation but i want to reset the gun to his rotation. Here is the script:
 #pragma strict
 
 var fireSpeed : float = 15;
 @HideInInspector
 var waitTilNextFire : float = 0;
 var bulletSpawn : Transform;
 var ShellSpawn : Transform;
 
 var MuzzleFlash : GameObject;
 var GunSound : GameObject;
 
 var Proiettile : Rigidbody;
 var Shell : Rigidbody;
 var ForzaSparo : int = 100;
 
 var BulletSpread : float = 0.5;
 var StartRotation;
 var lerpSpeed : float = 0.5;
  
 function Start()
 {
  StartRotation = transform.rotation;
  
 }
 function Update () 
 {
  var HoldSound: GameObject;
  var HoldMuzzleFlash : GameObject;
  var proiettileSparato : Rigidbody;
  var ShellSparato : Rigidbody;
  
  
  
   if(Input.GetButton("Fire1") )
   {
    
     
    if(waitTilNextFire <=0)
    { 
     if(GunSound)
      HoldSound = Instantiate( GunSound, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
      HoldMuzzleFlash = Instantiate( MuzzleFlash, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
     
      
       proiettileSparato = Instantiate(Proiettile, bulletSpawn.transform.position, bulletSpawn.transform.rotation); 
       transform.Rotate(Random.Range(-BulletSpread,BulletSpread),Random.Range(-BulletSpread, BulletSpread),0);
 
       waitTilNextFire = 1;
       ShellSparato = Instantiate(Shell, ShellSpawn.transform.position, ShellSpawn.transform.rotation); 
       ShellSparato.velocity = transform.TransformDirection( Vector3.right * Random.Range(10,20));
           
       proiettileSparato.AddForce(proiettileSparato.transform.forward * ForzaSparo); 
     }
   
     
 
  }
  
  
         transform.rotation = Quaternion.Lerp( transform.rotation, StartRotation, lerpSpeed);
   
   waitTilNextFire -= Time.deltaTime * fireSpeed;
   if(HoldSound)
   HoldSound.transform.parent = transform;
   if(HoldMuzzleFlash)
   HoldMuzzleFlash.transform.parent = transform;
   
 }
 
the description is unclear to me.
The problem is that it save the world space rotation but i want to reset the gun to his rotation
Who is "it"? Why does it "save" the rotation? Who is referred to by "his rotation"?
I guess the problem is Lerp() funtion
Lerp(from, to, float t) Interpolates between from and to by t. This means t SHOULD BE between 0 and 1, and it just interpolate. It would not increment for you.
Since your lerpSpeed is ALWAYS 0.5f, transform.rotation WOULD ALWAYS BE transform.rotation + ( StartRotation - transform.rotation) * 0.5f
Of course, you can not use Lerp() like this way.
Like Cherno, I also don't understand what "it save the world space rotation" means, so I just guess the problem... :P
If it's your problem, and you need example code, just tell :)
check localRotation and pivot point of your gun.
Gr33tz
Your answer
 
 
             Follow this Question
Related Questions
Rifle Accuracy w/ Rotation 3 Answers
Help with gun accuracy in degrees. 3 Answers
Weapon random movement 0 Answers
Gun accuracy with Random.Range? 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                