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
0
Question by moonLite · Apr 11, 2012 at 06:29 PM · if-statementsif-else

How to write a "or "statement & a if Else with or condition?

Hi Guys,

I have 2 questions,

1) How do i insert a Or Random Range insert this function?

     function ResetPosition ()
 {
     transform.position.y = 71;
     transform.position.x = Random.Range(-11 ,-10))||(Random.Range(10, 11);
     transform.position.z = 0;
     rigidbody.velocity = Vector3.zero;
 
 }


2) How do I add a or condition in the if else statement?

 if (randomNo >5)
 {
      transform.position.x -= speed * Time.deltaTime;
     if(transform.position.x < -6.1 )
     
     {
         //Instantiate(prefab, transform.position, Quaternion.identity);
         transform.position.y = Random.Range(-2.3, 1.4);
         transform.position. x = Random.Range (-5,5);
         transform.position.z = -3;
     }
 }
 
 else
 {
 transform.position.x += speed * Time.deltaTime;
     if(transform.position.x > 6.1 )
     
     {
         //Instantiate(prefab, transform.position, Quaternion.identity);
         transform.position.y = Random.Range(-2.3, 1.4);
         transform.position. x = Random.Range (-5,5);
         transform.position.z = -3;
     }
 
 }

I wish to combine <-6.1 & > 6.1 together.

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 GuyTidhar · Apr 11, 2012 at 06:47 PM 0
Share

Random.Range(10,11) always equals 10

Random.Range(-11,-10) always equals -11

Practically you did this:

transform.position.x = -11 || 10;

What do you wish to achieve?

2 Replies

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

Answer by gregzo · Apr 11, 2012 at 06:44 PM

Question 1)

I would do the following, altough I'm not 100% sure there is no more elegant way:

 function ResetPosition ()
 {
     transform.position.y = 71;
 
     if(Random.value<0.5)
     {
         transform.position.x = Random.Range(-11 ,-10);
     }
     else
     {
         transform.position.x = Random.Range(10, 11);
     }
     transform.position.z = 0;
     rigidbody.velocity = Vector3.zero;
 
 }
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 BiG · Apr 11, 2012 at 06:43 PM

1) It is not clear what you are trying to do here. Please explain.

2)

 if(transform.position.x > 6.1 || transform.position.x < -6.1)

This way, as you planned, you can combine the two branches of your if-statement into a single one.

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 moonLite · Apr 11, 2012 at 07:24 PM 0
Share

@gregzo & @BiG,

thanks! I wanted the enemies to randomly come from left & right screen.

So it's more like

      transform.position.x = -7 || 7 
     transform.position.y = Random.Range(-2.3, 1.4)

But i'm curious if we can have one "or" || statement with 2 sets of random.range values. e.g

   transform.position.x = Random.Range(-7,-4) || Random.Range(10,17) 

what should to code be like with 2 sets of random.range in the "or" || statement?

avatar image BiG · Apr 11, 2012 at 07:49 PM 0
Share

O$$anonymous$$, in that case, I think that gregzo has already guessed what you meant. With a 50% probability, you "pick" a value in the range (-7,-4); with the other 50% probability, you "pick" a value in the other range (10,17). You don't need a "||" operator here: just look at gregzo's answer, for the first question of yours, and modify the ranges with respect of your last comment: it should work fine!

avatar image moonLite · Apr 11, 2012 at 07:56 PM 1
Share

@BiG thanks again!

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

7 People are following this question.

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

Related Questions

I'm wondering with if function working 2 Answers

MouseLook half disabling 1 Answer

Power-Ups scripting error 1 Answer

Why is an action in my "if" block executing in the "else" conditions? 2 Answers

Efficient way to assign a value from another value 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