• 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 kurovskiy00 · Jan 27, 2019 at 06:35 PM · vector3randomvector3.up

Random with Vector3.up

Hello. My aim now is to use Random for choosing random Vector3 value, or rather Vector3.up\down\left\right. Example:

  if(timeleft < 0)
         {
             transform.Rotate(Vector3.left * Time.deltaTime * 30);  // here should be random value from left, right, up, down
             if(timeleft < -5)
             {
                 timeleft = 5f;
             }
         }

If I somehow can use Random.Range for playing random sound, I dont mind how it works with Vector3, please, help

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
0
Best Answer

Answer by kurovskiy00 · Jan 28, 2019 at 01:34 PM

Yeah, I solved it, just look at the result of my tries

     float timeleft;
     bool right = false;
     bool left = false;
 
     // Start is called before the first frame update
     void Start()
     {
         timeleft = 5f;
     }
 
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
 
             int randomPick = Random.Range(0, 2); //returns randomly 0,1.         
             switch (randomPick)
             {
                 case 0:
                     right = true;
                     break;
                 case 1:
                     left = true;
                     break;
                 default: break;
 
             }
         }
         if (right)
         {
             RotateRight();
         }
         if (left)
         {
             RotateLeft();
         }
         Debug.Log(timeleft);
     }
     void RotateRight()
     {
         transform.Rotate(Vector3.up * Time.deltaTime * 50);
         timeleft -= Time.deltaTime;
         if(timeleft < 0)
         {
             timeleft = 5f;
             right = false;
         }
     }
     void RotateLeft()
     {
         transform.Rotate(Vector3.down * Time.deltaTime * 50);
         timeleft -= Time.deltaTime;
         if (timeleft < 0)
         {
             timeleft = 5f;
             left = false;
         }
     }
 }
 

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 Turlok · Jan 27, 2019 at 07:05 PM

Random.Range simply returns a random number between the passed min and max parameters. Interestingly if you're passing floats, the max parameter is inclusive, but if you're passing integers the max parameter is exclusive.

Since you're using Vector3 shorthands you could try something like this:

 int randomPick = Random.Range(0,4); //returns randomly 0,1,2,3
 switch(randomPick){
 case 0:
 transform.Rotate(Vector3.left * Time.deltaTime * 30);
 break;
 case 1:
 transform.Rotate(Vector3.right* Time.deltaTime * 30);
 break;
 case 2:
 transform.Rotate(Vector3.up* Time.deltaTime * 30);
 break;
 case 3:
 transform.Rotate(Vector3.right* Time.deltaTime * 30);
 break;
 default: break;
 }


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 kurovskiy00 · Jan 27, 2019 at 07:45 PM 0
Share

Yes, thanks atleast your advice gave a result. But your script just rotate item left\right\up\down every frame. $$anonymous$$y aim is to make script choose one side of these and rotate during another function made it stop. I think after choosing one of this cases, another ones should shut off. Heh, i bet my idea tells nothing, because im not a programmer at all, but I've tried.

avatar image Turlok kurovskiy00 · Jan 27, 2019 at 07:54 PM 0
Share

It sounds like you're putting this code in the Update() function which is called every frame. Ins$$anonymous$$d declare your own function to call whenever you want to rotate the object.

avatar image kurovskiy00 Turlok · Jan 27, 2019 at 08:06 PM 0
Share

It rotates if I put this script not in every-frame function, but on very smal distance, it should rotate smoothly, so thats why it should be in every-frame function

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

125 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Modify mesh problems 1 Answer

Unity 2D How can I detect collision with a Vector3? 2 Answers

Vector3 not receiving coordinates 1 Answer

Spawn object from Random Vector3 in array 0 Answers

Vector 3 to float eg V3(1, 2, 3) -> float 123 in JS? 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