Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 kop4 · Jun 30, 2011 at 02:03 AM · rotationrandomturret

Reseting rotation of a random turret

I'm trying (and failing) to make a turret-like thingr that lobs a ball in a general forward direction.

I have a shoot function that is called every few seconds. It rotates the turret to a random range of degrees (-10,10), and then shoots. Obviously, when it shoots more than once, the rotations are "added" together (if the first random number is 9, and the second is 8, it ends up rotated 17 degrees).

I need a way to save the original rotation, and rotate the turret back to it after every shot. I tried making a start function to save the position, but it didn't seem to be working. here are the important parts of the script:

 function Start()
 {
     //Saves original rotation
     
     var startRotate = Quaternion;
 
 }
 
 
     
     
     function Shoot()
     {
     
             //rotating barrel
     
     
         var x = Random.Range(-10,10);
         var y = Random.Range(-10,10);
         var z = 0;
     
         transform.Rotate(x,y,z);
         print("rotated to shoot");
     
     
     
             //shooting
 var ball = Instantiate(ballPrefab ,SpawnPoint.transform.position , Quaternion.identity);
     ball.rigidbody.AddForce(transform.forward * ballforce);
     
     

 //rotating back
     var startx = startRotate[0];
     var starty = startRotate[1];
     var startz = startRotate[2];
     
     transform.Rotate(startx,starty,startz);
 
 }

What is wrong here? It should, in theory, rotate to a random place, shoot, and rotate back a fraction of a second later, but it's not. It just stays in its rotated position until the shoot function is called again.

Thanks,

Жaden

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by aldonaletto · Jun 30, 2011 at 04:41 AM

transform.rotation is a Quaternion, a strange creature with four dimensions, none of which corresponds to the familiar angles X, Y and Z we can see in the Rotation field in the Inspector (these are actually the local Euler angles ). You should save the transform.rotation in a variable outside any function, than restore it after shooting in the shoot function:

   private var startRotate:Quaternion;
 
   function Start(){
     startRotate = transform.rotation;
   }
 
   function Shoot(){
     // rotating barrel
     var x = Random.Range(-10,10);
     var y = Random.Range(-10,10);
     var z = 0;
     transform.Rotate(x,y,z);
     print("rotated to shoot");
     //shooting
     var ball = Instantiate(ballPrefab, SpawnPoint.transform.position,  Quaternion.identity);
     ball.rigidbody.AddForce(transform.forward * ballforce);
     yield WaitForSeconds(0.5); // wait 0.5 seconds
     // restore original rotation:
     transform.rotation = startRotate;
   }

The Shoot function will aim to some random direction +/- 10 degrees in the horizontal and vertical planes, shoot in that direction and return to the original rotation 0.5 seconds after.

Comment
Add comment · Show 3 · Share
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 aldonaletto · Jun 30, 2011 at 05:12 AM 0
Share

Additionally, you should destroy the bullet after some time, using this instruction right after the direction was restored:

   transform.rotation = startRotate;
   Destroy(ball,5); // destroy bullet after 5 seconds
avatar image kop4 · Jun 30, 2011 at 05:29 PM 0
Share

Thanks alot

avatar image Chris D · Jun 30, 2011 at 05:39 PM 1
Share

If al's solution worked for you, please mark his answer as accepted (the check mark by the vote up/down) so other can see that you've fixed things.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Can I randomize LookRotation? 1 Answer

Flip over an object (smooth transition) 3 Answers

Is rotation is close enough to shoot? 1 Answer

How can i add random range to an instantiated objects rotation? 1 Answer

Help clamping a Rotation 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