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 /
avatar image
0
Question by Zac_Mountain · Apr 23, 2017 at 09:58 PM · random.rangemathf.clamp

Random.Range producing values outside its range.

I've just started unity and I made a basic 2D game that I then converted to 3D, using the same code just reskinning some things, doing this, despite using the same code, my random.range spawned objects in the 120-degree area I had it working for, but in 3D this seems to be anywhere on the 360 degrees area.

As far as I'm aware there are no other cases of this happening I can find online.

The number generator:

 void RotRange (float startRot, float endRot){
 
         Vector3 rotation = transform.eulerAngles;
 
         rotation.z = Mathf.Clamp (Random.Range (startRot,endRot), startRot, endRot);
 
         print (rotation.z + " , " + pattenNum);
 
         //print (rotation.z);
 
         transform.eulerAngles = rotation;
     }

(startRot is 0f, endRot is 120f)

But even when I use mathf.clamp the values are still on a 360 spectrum and not been confined so i think there must be some external factor, I'm not sure if anyone knows what might cause this, and there is pretty limited information to go on but thank you in advance :)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by toddisarockstar · Apr 24, 2017 at 03:33 AM

I am surprised this is not giving errors like you have it. try like this:

 void RotRange (float startRot, float endRot){
         float f;
          f = UnityEngine.Random.Range (startRot,endRot);
          Vector3 rotation = new Vector3(transform.eulerAngles.x,
                                                                  transform.eulerAngles.y,
                                                                  f);
           print (rotation.z + " , " + pattenNum);
  
       
  
          transform.eulerAngles = rotation;
      }

in C# you cant assign just one coordinate to a Vector3. You have to assign the whole thing. you can read one at a time though.

Comment
Add comment · Show 1 · 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 Zac_Mountain · Apr 24, 2017 at 05:41 PM 0
Share

thanks! i think it worked becuase i was using a tempary value to store it in and then edited that before apllying the fulll Vecter3 back to the transform but like i said im no exspert, but as far as your aware this will set the rotation on the z axis between 0 and 120? thank you.

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

100 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 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 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

Can I generate values outside of the Random.insideUnitCircle?? 1 Answer

how to generate random number 1 to 10 without repeated any number if all number is generated then Restart Random unique number (C#) 0 Answers

Random.Range gives the same value in WebGL Build 0 Answers

Instantiating random prefabs gives wrong position 3 Answers

Random generated rooms in network multiplayer,Randomly generated rooms in mulitplayer 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