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 mihir solanki · Apr 28, 2015 at 11:20 PM · rotateaxiscubeleftright

Make a cube rotate in right or left direction randomly

i have a cube which i want to rote right or left on the x axis as shown in the diagram randomly.i have seen the euler angles and the rotation concept and some questions about them but have not found them suitable for my case.my code goes like this

 var x:float;
 function Start()
 {
   x=Random.value;
 }
 
 function Update()
 {
  if(x>=0.5)
 {
   //rotate right
 }
 if(x<0.5)
 {
  //rotate left
 
 }
 }

so i need the cube to rotate slowly towards right if the value is greater than 0.5 like in the pic attached and similarly for lesser values.Thankyou.

alt text

rot1.jpg (29.0 kB)
rot2.jpg (28.5 kB)
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

3 Replies

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

Answer by Addyarb · Apr 28, 2015 at 11:25 PM

Hey there, sorry you're having issues with rotation.

From what I understand, you just want to randomly rotate right or left.

Here's how to achieve that: (not tested)

  var rotateRight : bool;
 
 function Start()
 {
 var randomNumber = Random.Range(0,3); //Generates number between 1 & 2
 if(randomNumber > 1)
 rotateRight = true;
 else
 rotateRight = false;
 }
 
 function Update()
 {
 if(rotateRight)
 transform.Rotate(Vector3.right * Time.deltaTime);
 else
 transform.Rotate(Vector3.left * Time.deltaTime);
 }
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 mihir solanki · May 06, 2015 at 01:40 PM 0
Share

i need to stop it after some rotation at an angle like in the second pic.

avatar image CasperK · May 06, 2015 at 02:23 PM 0
Share

You could add a var to keep track of how much you've rotated.

 var angleToReach : float;

and decrease that while rotating while checking if its reached yet.

     if (angleToReach>0)
         { 
             angleToReach-=Time.deltaTime;//since the default vectors all have the length of 1 using just the deltaTime will work here.
             if(rotateRight)
                 transform.Rotate(Vector3.right * Time.deltaTime);
             else
                 transform.Rotate(Vector3.left * Time.deltaTime);
         }
avatar image mihir solanki · May 07, 2015 at 02:15 PM 0
Share

ok got it thanks

avatar image
0

Answer by TreeFish · Apr 29, 2015 at 02:37 AM

Use Rotate with a random range like so:

 public float speed = 10f; //set your speed
 Vector3 randomXDirection = new Vector3(Random.Range(-359, 359),0,0);
 
 void Update (){
 transform.Rotate(randomXDirection, speed * Time.deltaTime);
 }
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
avatar image
0

Answer by Chris333 · Apr 28, 2015 at 11:39 PM

Hi,

that works i tested it.

 #pragma strict
 
 var x:float;
 var speed:float = 2.0f;
 
 function Start()
 {
    x=Random.value;
 }
  
 function Update()
 {
       if(x>=0.5)
     {
            //rotate right
            transform.Rotate(-speed * Time.deltaTime, 0, 0);
     }
     
     if(x<0.5)
     {
           //rotate left
          transform.Rotate(speed * Time.deltaTime, 0, 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 Addyarb · Apr 28, 2015 at 11:46 PM 0
Share

Ooops, meant to write rotate. Good catch! I'm glad it works though. I'll update my code.

Is that what you wanted to achieve, though?

avatar image mihir solanki · May 06, 2015 at 01:39 PM 0
Share

yes but how do i stop it after some time at a certain angle like in the pic?

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

Pay animation if joystick pushed to left and another if joystick is pushed to right 2 Answers

Walking forward 2 Answers

How to make the npc face the player. 2 Answers

Auto walk 2 Answers

Is it possible to keep an object upright inside another game object that is spinning or rolling 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