• 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 Chimera3D · Jul 19, 2012 at 09:36 AM · raycastdirectionproceduralnumbercomparison

Numerical Comparison Logic

What I am trying to do is write some code that can do t$$anonymous$$s automatically and handle every possible conditions. How can I go about doing t$$anonymous$$s without writing t$$anonymous$$s code for every possible result.

 if(up == 1 && down == 1 && right == 0 && left == 0){
 
     randomizer = Random.Range(0,2);
     if(randomizer == 1){
         direction = 1;
     }else
         direction = 4;
 }
  
 if(up == 0 && down == 1 && right == 1 && left == 0){
  
     randomizer = Random.Range(0,2);
     if(randomizer == 1){     
         direction = 4;
     }else
         direction = 3;
 }
  
 if(up == 0 && down == 0 && right == 1 && left == 1){
  
     randomizer = Random.Range(0,2);
     if(randomizer == 1){
         direction = 2;
     }else
         direction = 3;
 }
  
 if(up == 0 && down == 1 && right == 0 && left == 1){
  
     randomizer = Random.Range(0,2);
     if(randomizer == 1){
         direction = 4;
     }else    
         direction = 2;
  
 }
Comment
Add comment · Show 5
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 Seth-Bergman · Jul 19, 2012 at 09:48 AM 0
Share
avatar image Chimera3D · Jul 19, 2012 at 08:38 PM 0
Share
avatar image Seth-Bergman · Jul 20, 2012 at 01:28 AM 0
Share
avatar image DaveA · Jul 20, 2012 at 01:33 AM 0
Share
avatar image Chimera3D · Jul 20, 2012 at 01:36 AM 0
Share

2 Replies

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

Answer by Piflik · Jul 19, 2012 at 09:43 PM

Make the four raycasts, if they don't $$anonymous$$t, you store that direction in an array and increase a variable by one. After the four raycasts you chose randomly one of the entries in the array.

Pseudocode:

 private var directions : int = new int[4];
 
 function chooseDirection() {
 
     var dirNmbr : int = 0;
 
     for(var i = 0; i < 4; i++) {
         //raycast in direction i
         if(!raycast.$$anonymous$$t) {
             directions[dirNmbr] = raycast;
             dirNmbr++;
         }
     }
 
     if(dirNmbr > 0) {
         direction = directions[Random.Range(0, dirNmbr)]
     }
 }  
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 Seth-Bergman · Jul 19, 2012 at 09:54 AM

Assuming you want a random direction:

var moveToNextSpot = false;

function ChooseDirection(){

 var randomDir = Random.Range(1,4);
 
 if(randomDir == 1)
 {
     if (!Physics.Raycast (transform.position, (Vector3.up), awareness)) 
     moveDir = Vector3.forward;   
 }
 
 if(randomDir == 2)
 {
     if (!Physics.Raycast (transform.position, (Vector3.right), awareness)) 
     moveDir = Vector3.right;    
 }
 
 if(randomDir == 3)
 {
     if (!Physics.Raycast (transform.position, (-Vector3.right), awareness)) 
     moveDir = -Vector3.right;    
 }
 
 if(randomDir == 4)
 {
     if (!Physics.Raycast (transform.position, (Vector3.up), awareness)) 
     moveDir = -Vector3.up;    
 }
 moveToNextSpot = true;

}

 function Start (){
 ChooseDirection();
 }
 
 function Update(){
     if(moveToNextSpot)
     transform.Translate((moveDir) * Time.deltaTime);
     else
     ChooseDirection();
 }

then you need to decide when the object has reached it's next point to check direction again, and turn the boolean back to false...

EDIT: updated code for relevance

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

7 People are following this question.

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

Related Questions

Generate mesh from raycast positions, independent of rotations 0 Answers

Negative raycast direction always returns true 1 Answer

Why i can't see my questions ? 2 Answers

Skyboxes, GetPixel and Raycast Directions 1 Answer

How to incorporate a rotation towards mouse position in this script? I tried 0 Answers


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