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 heaversm · Jul 13, 2011 at 08:56 PM · rotationrandom

rotation of an object relative to its original position

Hi - just wondered how I might rotate an object by a random degree within a certain range, but always relative to its initial starting position. I have this code, which is working, but seems to rotate relative to the objects current rotation:

 var randomAngle:float = Random.Range(-30, 30);
 launcherRef.transform.Rotate(Vector3.up * randomAngle);
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

2 Replies

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

Answer by aldonaletto · Jul 13, 2011 at 09:32 PM

You can save the initial rotation at Start(), create the random angle and add it to the initial rotation - but be aware that rotations are quaternions, misterious creatures where X-Y-Z-W have nothing to do with the familiar angles we know, so we must use the quaternion right operations:

 var initialRot: Quaternion;
 
 function Start(  // save the initial rotation
   initialRot = launcherRef.transform.rotation;
 }
 
 // when you want to rotate the launcherRef do the following:
   // create a random rotation around Y axis
   var randomRot = Quaternion.Euler(0,Random.Range(-30, 30),0);
   // combine the initial rotation with the random one:
   launcherRef.transform.rotation = initialRot * randomRot;
   ...
  
Comment
Add comment · Show 5 · 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 heaversm · Jul 13, 2011 at 09:44 PM 0
Share

So strange. That does seem to work. Why is it initialRot * randomRot ins$$anonymous$$d of initialRot + randomRot. I would think if your object had an initialRot of 150 degrees and you generated a randomRot of 30 degrees, that would give you a rotation of of 4500 degrees.

avatar image aldonaletto · Jul 13, 2011 at 10:41 PM 0
Share

I agree with you: the + operator would be much more intuitive, because we are actually adding rotations (at least that's what it seems to us). But the convention adopted in Unity (maybe everywhere, I'm not sure) is the operator, which for Quaternions means combine, not multiply. Internally, Unity may execute a lot of other operations to effectively combine both rotations, not just a simple multiplication. And there is an additional detail: a b and b * a may give different results in the quaternion world. I've never tested it, but the docs warn us about this.

avatar image Waz · Jul 13, 2011 at 10:47 PM 0
Share

Turning left then laying on you back gives different results to laying on your back then turning left, so it is with Quaternion multiply order.

avatar image heaversm · Jul 14, 2011 at 10:09 AM 0
Share

You lost me at "Quaternion".

avatar image aldonaletto · Jul 14, 2011 at 11:57 AM 0
Share

Thanks, @Warwick Allison, it's a good example.

avatar image
0

Answer by Waz · Jul 13, 2011 at 09:28 PM

That code will rotate around the global Y (up) axis. If you want the local up, use transform.up, not Vector3.up.

Comment
Add comment · Show 2 · 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 heaversm · Jul 13, 2011 at 09:36 PM 0
Share

Thanks. I think I do want the global Y, actually, but it just needs to be relative to a starting point. So my launcher is set at 150 degrees, and I want it to fire between 120 and 180 degrees on the global access. I tried this, but it still doesn't seem to be quite right:

 var randomAngle:float = Random.Range(120, 180);
     launcherRef.transform.Rotate(Vector3.up * randomAngle);
avatar image rbnzdave · May 13, 2014 at 10:26 AM 0
Share

plus transform.Rotate is specifically for modifying the current rotation, as opposed to the absolute you deal with when using rotation. Rotate() is a relative change.

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

How can I make the keys randomly change after each keypress? 1 Answer

A node in a childnode? 1 Answer

Flip over an object (smooth transition) 3 Answers

Rotation script 2 Answers

C# GameObject Reverses Z Rotation 0 Answers


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