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;
}
↧