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 rednax20 · Apr 03, 2013 at 09:37 PM · rotationtransformupdateyx

Please help. I can't stop this light from moving!!!

I have a spotlight that moves around randomly. It needs to not exit the map, I created a script to make sure it doesn't leave some coordinates. If it does it moves it appropriately in the direction away from where it is.

 var Mother : GameObject;
 function Update (){
     if(Mother.position.x >= 116){
         Mother.transform.position += (Vector3(-1,0,0));
     }
     if(Mother.position.x <= -80){
         Mother.transform.position += (Vector3(1,0,0));
     }
     if(Mother.position.y >= 110){
         Mother.transform.position += (Vector3(0,-1,0));
     }
     if(Mother.position.y <= -65){
         Mother.transform.position += (Vector3(0,1,0));
     }
 }

Is does not work. I am very upset. If you know how to do this please give me someway to do this. whether its a script you made or a place that teaches you how to do this, the projects due next week. thank you so much for your help.

Comment
Add comment · Show 2
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 Benproductions1 · Apr 03, 2013 at 09:55 PM 0
Share

What you want is $$anonymous$$athf.Clamp

avatar image rednax20 · Apr 03, 2013 at 10:22 PM 0
Share

could you elaborate please. I don't know what that does, when I put the example script from unity reference manual (with my coordinates) the spotlight just dissapeared. I even tried position.x ins$$anonymous$$d of Time.time, but that didn't do any thing.

 var $$anonymous$$other : GameObject;
 function Update () {
     $$anonymous$$other.transform.position = Vector3($$anonymous$$athf.Clamp(Time.time, 0.0, 116.0), 0, 0);
 }

 var $$anonymous$$other : GameObject;
 function Update () {
     $$anonymous$$other.transform.position = Vector3($$anonymous$$athf.Clamp($$anonymous$$other.position.x, 0.0, 116.0), 0, 0);
 }

I need help using this!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Benproductions1 · Apr 03, 2013 at 10:44 PM

Ok, Lets say you want to keep the X value of a Vector3 between a maximum and a minimum value

You can simply go this: //The variables we will use var vector:Vector3; var maximum:float; var minimum:float;

 //Now to clamp the x value between maximum and minimum
 //so:
 vector.x = Mathf.Clamp(vector.x, minimum, maximum);
 //This will keep vector.x between the value of minimum and maximum
 
 //You can do this for all other components of the vector too
 vector.y = Mathf.Clamp(vector.y, minimum, maximum);
 //etc...

The problem with your code in the comments are: 1. why are you clamping Time? WTF?? 2. Your trying to clamp the position variable of a GameObject, but a GameObject has no position, it has a transform component with a position

Instead you should do this:

 function Update() {
     Mother.transform.position.x = Mathf.Clamp(Mother.transform.position.x, minimum, maximum)
 }

Hope this helps, Benproductions1

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.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Rotation Numbers Are Off? 0 Answers

How to keep a Gameobject in the same position after a transform.Rotate? 2 Answers

y and z rotation axis doing the same thing when x is 270 degrees 0 Answers

Updating values in Update function 0 Answers

Quaternions acting up 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