• Unity
  • Services
  • Made with Unity
  • Learn
  • 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
  • Forums
  • Answers
  • Feedback
  • Issue Tracker
  • Blog
  • Evangelists
  • User Groups

Navigation

  • Home
  • Unity
  • Industries
  • Made with Unity
  • Learn
  • Community
    • Forums
    • Answers
    • Feedback
    • Issue Tracker
    • Blog
    • Evangelists
    • User Groups
  • Get Unity
  • Asset Store

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 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 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 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 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. My 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 at 07:54 PM 0
Share

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

avatar image kurovskiy00 Turlok · Jan 27 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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

Spawning at a random position away from the player 1 Answer

Transform.position without Vector3? 1 Answer

How to get random point on the perimeter of a rectangle? 1 Answer

Linear interpolating (Lerping) a Gameobject between random positions of Other GameObjects 1 Answer

Random spawn script not working correctly 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges