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
2
Question by Sericet1 · Oct 29, 2013 at 09:19 PM · rotate object

Random z rotation instantiate

I am trying to spawn an object in my scene but I want it to have a random z rotation when it is created. I thought about simply applying a script to it...like so.

[code]

void Awake() { transform.rotation.z = Random.Range(0,360); } [/code]

I think the cleaner and best way would be to instantiate it with a random rotation in the z axis. I know Quaternion.Random will produce a random rotation in all axis, but is there a way to simple do it to the z axis only?

I am not familiar with quaternions so any explanations would be appreciated. Currently trying to figure them out instead of avoiding them.

Comment
Add comment · Show 2
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 tanoshimi · Oct 29, 2013 at 09:31 PM 0
Share

I admire your desire to understand and work with Quaternions, but having tried myself to wade through this explanation of how they work, I decided to stick with Euler angles... in fact, the Unity documentation itself recommends that "you almost never access or modify individual Quaternion components".

I personally don't see anything particularly unclean about your current approach :)

avatar image Sericet1 · Oct 29, 2013 at 09:38 PM 0
Share

How should I go about instantiating my object so that it has a random orientation in the z axis? Is there a way to do that with euler angles?

2 Replies

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

Answer by robertbu · Oct 29, 2013 at 09:41 PM

A transform.rotation is a Quaternion. The x,y,z,w values range from 0.0 to 1.0 and are not intuitive. There are a number of ways to get what you want here. In part the solution will depend on whether you have any unknown x and/or y rotation.

For applying a random rotation on the 'z' to an unknown rotation:

 void Awake() {
     transform.Rotate(0.0, 0.0, Random.Range(0.0, 360.0));
 }

This is a relative, rotation around the local 'Z' axis.

If you don't have any rotation on the x or y, or if you know the rotation, then you can put it directly in the Instantiate():

 Instantiate(somePrefab, somePosition, Quaternion.Euler(0.0, 0.0, Random.Range(0.0, 360.0));
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 Sericet1 · Oct 29, 2013 at 09:49 PM 0
Share

Excellent answer, thank you. So let me see if I have this right...Quaternions range from 0,1 and by doing Quaternion.Euler you are able to hand it 0-360 and it does the conversions for me?

avatar image robertbu · Oct 29, 2013 at 09:55 PM 0
Share

Yes. As @tanoshimi says, the reference manual recommends against manipulating Quaternion components directly. There are a variety of helper functions that can be used in a large number of ways to solve rotation problem. For example, here are alternate solutions:

 Instantiate(somePrefab, somePosition, Quaternion.AngleAxis(Random.Range(0.0,360.0), Vector3.up));

And here is one for a relative rotation:

 transform.rotation = Quaternion.AngleAxis(Random.Range(0.0,360.0), transform.up)) * transform.rotation;
avatar image
0

Answer by pater2003 · Nov 03, 2018 at 05:40 PM

Thanks ;)

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

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help platform rotating on x-Axis passes through colliders 0 Answers

My tank will flips on the air when it collide other objects. 1 Answer

Issue moving Object around surface of sphere 3 Answers

Animated objects rotation is flawed. 1 Answer

How do I rotate an object over time outside the Update method? 2 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