• 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
1
Question by bombardier · Apr 01, 2012 at 02:22 AM · transform.rotate

transform.Rotate not rotating with conditional statement

I have an object flying around on my screen and I want it to vary its flight path randomly. I have it programmed to continuously fly straight and then test an IF statement to see if it rotates up or down. The "else" of my if statement is never happening.

     function Update () {
     
         transform.Translate(Vector3.forward * (Time.deltaTime*50));
 
 Random.seed = Random.Range(0, 10000);
     
         if ((Random.Range(0, 10))>5)
         {
             transform.Rotate(Vector3.up * (Time.deltaTime*50));
         }
         else
         {
             transform.Rotate(Vector3.up *(-1 * (Time.deltaTime*50)));
         
         }
     }

I assume this is a really dumb logic error, but Im apparently not seeing it.

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

Answer by aldonaletto · Apr 01, 2012 at 04:39 AM

As @Eric5h5 said, the object will rotate almost every frame, what doesn't produce a noticeable rotating effect - the object just shakes wildly. A better approach would be to turn it left during a random time, then right during another random time, and so on:

private var timeToChange:float = 0.0; private var speed = 50.0; // 50 degrees per second

function Update () { transform.Translate(Vector3.forward * (Time.deltaTime*50.0)); if (Time.time > timeToChange){ // when random time elapsed... // draw a new random time between 0.3s and 1.3s timeToChange = Time.time+0.3+Random.value; speed = -speed; // and revert direction } transform.Rotate(Vector3.up*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
1

Answer by Eric5h5 · Apr 01, 2012 at 03:09 AM

It's just going to jitter randomly right or left every frame, with a 6/10 chance of the first Rotate, and a 4/10 chance of the second, so on average it will rotate right more often than left. Perhaps you meant "Random.value > .5", although I'm not really sure what you're trying to do exactly. Also, setting Random.seed every frame is pointless.

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
1

Answer by motionsmith · Apr 01, 2012 at 03:42 AM

Yeah, I'm thinking it probably has something to do with your seed. I wouldn't bother with that line of code.

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.

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

6 People are following this question.

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

Related Questions

Turn page, smooth rotate 1 Answer

unity how to make objects look at random objects with the same tag 1 Answer

How to stop a child from orbiting around parent? 0 Answers

Issues with copying Y axisrotation of another object 2 Answers

Transform.rotation.y rotating around x and z... 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