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 Apr 25, 2018 at 11:49 PM by S_jay1 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by S_jay1 · Feb 26, 2018 at 11:24 PM · playertransform.position

How do I change the transform position of my player when it reaches a certain x-position?

I want to make my player's x-position change to a different one when it hits a certain x-position. For example, when my player's x-position is less than -10.0f, I want it to become 10.0f. This is my script so far, but it's not working:

     void Update () { 
 
 
         if (transform.position.x < -10.0ff) {
             transform.position = new Vector3 (10.0f, transform.position.y, transform.position.z);
         }
         if (transform.position.x > 10.0ff) {
             transform.position = new Vector3 (-10.01f, transform.position.y, transform.position.z);
         }
     }
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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by FuzzyLogic · Feb 26, 2018 at 11:56 PM

Unity should be giving you an error about "unexpected symbol 'f'" because of lines 4 and 7 in your sample code. Otherwise, it should work, although it would be better to have if/else if instead of two separate if branches since they are exclusive.

 void Update () { 
     if (transform.position.x < -10.0f) {
         transform.position = new Vector3 (10.0f, transform.position.y, transform.position.z);
     } else if (transform.position.x > 10.0f) {
         transform.position = new Vector3 (-10.01f, transform.position.y, transform.position.z);
     }
 }

It's not an error but you probably want to add/subtract 20.0f to/from position.x instead of just setting x to min/max. Consider if position.x is -10.5f. When you wrap around, you generally want to retain the remainder beyond -10.0f (-0.5f) so your new position would be 9.5f instead of 10.0f. Otherwise your object may appear to stall when it wraps around, moving only a fraction of the distance that was intended.

 void Update () { 
     if (transform.position.x < -10.0f) {
         transform.position = new Vector3 (transform.position.x + 20.0f, transform.position.y, transform.position.z);
     } else if (transform.position.x > 10.0f) {
         transform.position = new Vector3 (transform.position.x - 20.0f, transform.position.y, transform.position.z);
     }
 }
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 S_jay1 · Feb 27, 2018 at 12:15 AM 0
Share

This is what I needed, thank you!

avatar image
0

Answer by Chik3r · Feb 26, 2018 at 11:41 PM

Hello, @S_jay1 Replace :

 if (transform.position.x < -10.0ff) {
              transform.position = new Vector3 (10.0f, transform.position.y, transform.position.z);
          }
          if (transform.position.x > 10.0ff) {
              transform.position = new Vector3 (-10.01f, transform.position.y, transform.position.z);
          }

With :

 transform.position = new Vector3(Mathf.Clamp(transform.position.x, -10f, 10f), transform.position.y, transform.position.z);

What Mathf.Clamp does?

Mathf.Clamp will check if a value is less than the minimum(-10f here) and return the minimum if its lower, and check if a value is higher tha the maximum(10f here) and return the maximum if its higher.

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 FuzzyLogic · Feb 26, 2018 at 11:57 PM 0
Share

This is not the same behaviour. The op wants wrap, not clamp.

avatar image S_jay1 · Feb 27, 2018 at 12:08 AM 0
Share

For some reason, this script isn't working. The player is not moving back to the -10 position. It is just constantly moving forward. Is there something wrong with my script?

 void Update () { 
 
         GetComponent<Rigidbody> ().velocity = new Vector3 (2, 0, 0); 
 //I'm using this just to move the player foward
 
 transform.position = new Vector3($$anonymous$$athf.Clamp(transform.position.x, -10f, 10f), transform.position.y, transform.position.z);
     }
avatar image S_jay1 S_jay1 · Feb 27, 2018 at 12:14 AM 0
Share

I didn't even realize that the player was attached to a Canvas. This caused the script to not work at all. The clamp freezes my player to a certain position, I want to loop back. Thank you for showing me how a clamp works though!

Follow this Question

Answers Answers and Comments

87 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

Related Questions

smooth Movement script for Player prefab in Steam VR using Oculus Rift touch controllers? 1 Answer

[C#] [2D] Not changing positions correctly 1 Answer

Player Can Not get off platform 1 Answer

Triggering Video Player with OSC (UniOSC and QLab),Trigger Video Player with OSC (UniOSC and QLab) 1 Answer

How do I get a character to walk on walls and ceilings? 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