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
1
Question by Hektor · Apr 27, 2011 at 10:06 AM · rotationrandomrange

Random rotation

Hello, in my scene there are some Transforms who must turn in a random direction when they pass beyond some areas. I made this script.

var speed : int = 5

     function Update () 
 {   
     transform.Translate(Vector3.left*speed/10);

     if((transform.position.z <= 15)||(transform.position.z >= 1985)||(transform.position.x <= 15)||(transform.position.x >= 1985)) 

     {dontFall();}

 }


 function dontFall()
 {
     var choice : float = Random.Range(100,-100); 
     if(choice < 0)
     {leftRot();}
     else
     {rightRot();}
 }

 function leftRot()
 {transform.Rotate(0,speed * 2,0);}

 function rightRot()
 {transform.Rotate(0,speed * -2,0);}

Do not give me error messages, but the variable choice is constantly changing and the transform turns both right and left. How can I fix it?

-H

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 Kacer · Apr 27, 2011 at 10:14 AM 0
Share

im sorry if im being a retard, but it seems to me that the script is doing exactly what you want it to do, can you please elaborate?

2 Replies

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

Answer by Joshua · Apr 27, 2011 at 10:43 AM

Hey Hektor, I can see you're new to scripting so one little thing, you say:

{dontFall();}

When calling a function there is no need for those brackets. You can just leave them out.

Then you have leftRot and rightRot. If your object it out of bounds you constantly pick a random number and then rotate that way. You want to once pick a random number and then constantly rotate that way. Add a boolean that checks if you've done the random picking. So on top of your script put var randomised : boolean = false; and then where if now says

{
    var choice : float = Random.Range(100,-100); 
    if(choice < 0)
    {leftRot();}
    else
    {rightRot();}
}

It should say:

if(!randomised){ var choice : float = Random.Range(100,-100); randomised = true; if(choice < 0) leftRot(); else rightRot();

}

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 Hektor · Apr 27, 2011 at 02:36 PM 0
Share

Thank you very much for the help, the script now works perfectly!

avatar image flaviusxvii · Apr 27, 2011 at 05:19 PM 1
Share

Years of program$$anonymous$$g have trained me to ALWAYS use brackets for things like if statement blocks. Beginners don't realize the implications of no braces and often try to add more to the conditional block forgetting that only the first statement gets executed if brackets are missing. Heed my warning and save hours of debugging!

Irony alert: $$anonymous$$y favorite language is Python!

avatar image Joshua · Apr 28, 2011 at 12:20 AM 0
Share

True, fair enough. To be honest, I also nearly always force myself to use them. The way he did it looks weird to me though :p samec line 'n stuff.

avatar image
0

Answer by Cyb3rManiak · Apr 27, 2011 at 10:31 AM

You should turn leftRot() and rightRot() into Coroutines. The idea is that right now you randomly choose the direction each frame, when what you want is when the object enters the area - decide the direction, and make a method run in that direction until you tell it to stop.

This should give you an idea of how to proceed:

var defaultSpeed : int = 5 var randomRotating: boolean = false;

function Update () {
transform.Translate(Vector3.left*speed/10);

 if((transform.position.z &lt;= 15)||(transform.position.z &gt;= 1985)||(transform.position.x &lt;= 15)||(transform.position.x &gt;= 1985)) 
 {
     if (!randomRotating)
         dontFall();
 }
 else if (randomRotating)
 {
     randomRotating = false;
     StopCoroutine("Rotate");
 }

}

function dontFall() { var choice : float = Random.Range(100,-100); if(choice < 0) { StartCoroutine("Rotate", defaultSpeed); } else { StartCoroutine("Rotate", -defaultSpeed); } }

function Rotate(float speed) { randomRotating = true;

 while (randomRotating)
 {
     transform.Rotate(0, speed * 2, 0);
     yield;
 }

}

As a side note, you better check OnTriggerStay and the physics of Unity in general, since checking if your object is in the range you want every frame is not efficient at all...

[Edit: I've revised the answer in case someone ever stumbles upon it again. Stupid rookie mistake on my part. Forgetting to add a while loop with a "yield" inside Rotate()... stupid stupid stupid :/]

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 Hektor · Apr 27, 2011 at 02:33 PM 0
Share

I'm sorry, but the script does not work well. The variable 'choice' still change every frame. Thanks anyway for your time.

avatar image Cyb3rManiak · Apr 28, 2011 at 03:17 PM 0
Share

Yup, my bad. Sorry about that :/ I've edited the answer to correct it.

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

No one has followed this question yet.

Related Questions

Random Rotation With Limited Range? 2 Answers

How can i add random range to an instantiated objects rotation? 1 Answer

Make a camera collision 1 Answer

How to change the rotation of the circle spawn? 1 Answer

Randomize shape.rotation for a particle emitter shape, sub emitter instance 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