Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Jan 28, 2016 at 10:07 PM by viv2061 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by viv2061 · Jan 26, 2016 at 12:47 AM · scripting problem

Scripting error

From what I understand in this script, the gameobject will relocate itself to another random spot if the position is greater than -15 AND less than 15. In other words, it will relocate itself whenever the position is between -15 and 15.

To test this, I made the script print out its location after relocating itself. But sometimes, the position is between -15 and 15. How come?

        public float mxpos = 45;
        public float mnpos = 15;
        void Start () {
                transform.position = new Vector2 (Random.Range (mxpos, -mxpos), Random.Range (mxpos, -mxpos));
        
                while (transform.position.x > -mnpos && transform.position.x < mnpos && transform.position.y > -mnpos && transform.position.y < mnpos) {
                    transform.position = new Vector2 (Random.Range (mxpos, -mxpos), Random.Range (mxpos, -mxpos));
                }
        print(transform.position);
                rb2d.velocity = new Vector2 (transform.position.x, transform.position.y);
            }

I feel like I'm missing something, but I can't find it. Can anybody help? Thanks in advance.

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 brunocoimbra · Jan 26, 2016 at 12:26 PM 0
Share

Position is a Vector, so you want it to teleport when it is between (-15, -15) and (15, 15), right? What is the current output?

avatar image viv2061 · Jan 26, 2016 at 11:14 PM 0
Share

Yes, whenever it is between (-15, -15) and (15,15), I need it to move somewhere else. Currently though, the output is ranges from (-45, -45) to (45, 45). But sometimes, it ends up between (-15, -15) and (15,15). I want the script to teleport the gameobject somewhere between (-45, -45) and (45, 45) but not between (-15, -15) and (15, 15).

avatar image bubzy viv2061 · Jan 26, 2016 at 11:28 PM 0
Share

you could make mxpos = 30; then

transform.position = new Vector2 (Random.Range ((mxpos+mnpos), -(mxpos+mnpos)), Random.Range ((mxpos+mnpos), -(mxpos+mnpos)));

that would make sure that it very rarely spawns in the -15 to 15 position (unless the random number generator comes up with a 0) if that is a problem, just add a 1 in the equation :) hope this helps a bit.

avatar image viv2061 · Jan 26, 2016 at 11:45 PM 0
Share

@buzby But wouldn't that just make it? :

 transform.position = new Vector2 (Random.Range (45, -45), Random.Range (45, -45));

since it is just doing 30+15?

avatar image bubzy viv2061 · Jan 27, 2016 at 10:47 PM 0
Share

yes it would. and when im not quite so drunk, ill rewrite. basically though, you gotta add the + and -15 values after you generate the random number.

1 Reply

  • Sort: 
avatar image
0

Answer by brunocoimbra · Jan 28, 2016 at 05:08 AM

After reading the @bubzy last reply, here is the code:

 transform.position = new Vector2(
     Mathf.Sign(Random.Range(-1, 1)) * (Random.Range(0, mxpos - mnpos) + mnpos),
     Mathf.Sign(Random.Range(-1, 1)) * (Random.Range(0, mxpos - mnpos) + mnpos));

EDIT

Just to explain what is happening:

Mathf.Sign will return -1 if it receives a negative number and 1 if it receives a positive number OR zero.

Note also that when using "ints" in Random.Range, the max number is exclusive, so in the Random.Range(-1, 1) will always generate -1 or 0 (1 is not included, generating a 50-50 chance).

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

Follow this Question

Answers Answers and Comments

44 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

Related Questions

How can I make a UI button's action happen before `Input.GetMouseButtonDown` action in another script? 0 Answers

The requested feature is not implemented. 0 Answers

Bunch of instantiated RBs blows away 0 Answers

SetSpeed float C# 1 Answer

How to find all AudioClips in Resources folder and play them? 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