Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Master Productions · May 10, 2015 at 10:22 PM · rotationrandomgunweaponaccuracy

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;
   
 }
 
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · May 10, 2015 at 11:13 PM 0
Share

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"?

avatar image daleth90 · May 11, 2015 at 02:42 AM 0
Share

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 :)

avatar image Code_Nashor · May 11, 2015 at 12:14 PM 0
Share

check localRotation and pivot point of your gun.

Gr33tz

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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

two Random.Range returning similar values 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges