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 /
avatar image
0
Question by Digital-Phantom · Apr 15, 2018 at 10:22 AM · unity 5movementpositionclamprestrictmovement

Why is Clamp changing objects starting position ?

I have a simple line of code to move an object left and right using the a, d keys, which works perfectly - public class PlayerMovement : MonoBehaviour { public int speed = 0;

     void Update()
     {
         transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * speed, 0.0f, 0.0f);
     }
 }

However, when I try to restrict the objects movement using Clamp it changes the objects starting position. The player/object is set to origin (0,0,0) but when the game starts its position is now whatever value Ive set maxX to in the inspector. Its always starting to the left so If I assign maxX as 10 its starting position is now -10 from origin. The game is 3d and there are no animations. I'm not getting any errors, but I don't understand why the object no longer starts at origin?

 public class PlayerMovement : MonoBehaviour
 {
     public int speed = 0;
     public float maxX;
 
     void Update()
     {
         transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * speed, 0.0f, 0.0f);
 
         Vector3 pos = transform.position;
         pos.x = Mathf.Clamp(pos.x, -maxX, maxX);
         transform.position = pos;
     }
 }

Any Ideas???

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 Bunny83 · Apr 15, 2018 at 10:48 AM

What do you mean by "reset"? You restrict the movement to a world space position range of -maxX to +maxX. What value have you maxX set to and what position does your player have in the editor and what position do you get when you start the game? My guess is that your player is already outside your specified range and it get of course moved into the range once the clamp runs.


You should include your numbers in the question. Maybe a screenshot? Is it a 2d or 3d game? Where is the camera located and where does it look at? Does your player have an animation / animator component? Maybe you have an empty animation with one frame at the 0,0,0 position?


We only see what you show us and what we know at the moment is not enough to answer this question. We can only guess at this point.

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 Digital-Phantom · Apr 15, 2018 at 11:59 AM 0
Share

Sorry, badly worded explaination. Changed title and description hopefully more helpful

avatar image Bunny83 Digital-Phantom · Apr 15, 2018 at 12:06 PM 0
Share

Are you sure about your position? Try adding this in Start:

 void Start()
 {
     Debug.Log("Start position: " + transform.position);
     Debug.Log("maxX: " + maxX);
 }

If your object is a child object keep in $$anonymous$$d that the position shown in the inspector is the localPosition, not the world space position. I'm still quite sure that your object is not really in the worldspace range of -10 to 10 at start but most likely more to the left.


If that's not the case you may have another script that tinkers with the position of your player.

avatar image Digital-Phantom · Apr 15, 2018 at 12:49 PM 0
Share

I should try more sleep and less caffeine, I can see where my explanation is confusing you, sorry.

$$anonymous$$y terrain is a 70x70 square, my player object is set (at Start) in position +35 on the X axis or exactly hallway across the board. (I've been thinking as this of centre.)

Your Debug.Log shows "Start position = 35.0, 2.0, 2.0)" and "maxX = 10"

At game start this is placing my game object at -10 on the X axis and it allows me to travel another 10 to the left and nothing to the right

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

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

stopping the gameObject if it is out of specified bounds. 1 Answer

How to cap this movement? 1 Answer

Copy position from one object to another on one axis 0 Answers

Clamping object movement in circle 1 Answer

How can i make my enemy jump with a delay 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