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 PatrickNeary · Jan 28, 2013 at 07:48 PM · rigidbodytranslaterandom.rangeaddtorque

how to use random.range to switch switch facing of a rigidbody between z+,- and x+,-

wondering how to use random.Range , to switch the facing of an object between z+ or - and x+ or - . i know that i will also have to use Vector3. i know i will also have to use addtorque as the object i want to apply this translation to is a rigid body. so any input you guys could give me would be amazing, as i just cant get my head around this, thank you in advance!
The context in which i wish to use this script , is a cube primitive, which if clicked, will randomly face a different direction . it already has a force applied in the forward axis (z+)

Comment
Add comment · Show 1
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 robertbu · Jan 28, 2013 at 10:32 PM 0
Share

Are you trying to turn the cube so that it drives in a new direction, or is the cube turning independent of the force you've already applied?

2 Replies

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

Answer by Loius · Jan 28, 2013 at 08:22 PM

If you want it to just be directly along one of the axes, you'd pick a random number (from four) and use that to determine which direction:

 var dir : int = Random.Range(0,4);
 var tx : Transform = cube.transform;
 
 switch( dir ) {
  case 0: tx.forward = Vector3.right;   break;
  case 1: tx.forward =-Vector3.right;   break;
  case 2: tx.forward = Vector3.forward; break;
  case 3: tx.forward =-Vector3.forward; break;
 }

If you want any facing at all along the X/Z plane:

 var newDir : Vector3 = Random.insideUnitSphere;
 newDir.y = 0;
 cube.transform.forward = newDir;
Comment
Add comment · Show 1 · 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 PatrickNeary · Jan 28, 2013 at 09:11 PM 0
Share

thank you so much, this site is amazing !

avatar image
0

Answer by PatrickNeary · Jan 29, 2013 at 10:51 AM

this ended up working perfectly, cheers :D

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

10 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

Related Questions

Why isn't AddTorque working on this character? 0 Answers

AddTorque doesn't rotate? 3 Answers

Rigidbody.AddTorque/AddForce Question. 1 Answer

Character - Collider With Scene 0 Answers

Rigidbody.MovePosition relativeTo parent 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