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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by TheDavyStar · Jun 27, 2015 at 12:15 AM · c#2dplayergame

How do I stop the player from moving offscreen?

So I'm making a top-down shooter in 2D mode where the camera has a fixed position. I want to make sure that whenever the player moves a crtain amount of pixels to the left or right, they can't go beyond that point. So far I've been trying (and failing) to achieve this by using this code:

 if(transform.position.x > 300)
                 
             {
                 transform.position.x = 300;        
             }
             
         if(transform.position.x < -300)
                 
         {
                 
                 transform.position.x = 300;            
         }


When I build the code in MonoDevelop I get an error reading "Cannot modify the return value of transform.position because it is not a variable." So far I haven't been able to find a workaround for this. Any help would be greatly appreciated.

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

· Add your reply
  • Sort: 
avatar image
3

Answer by zach-r-d · Jun 27, 2015 at 12:45 AM

Long story short, because transform.position is a property that returns a value type, a temporary copy is made, and automatically created temporary copies cannot be modified. The solution is to manually create a temporary copy, modify it, then assign it back:

 if (transform.position.x > 300) {
     Vector3 pos = transform.position;
     pos.x = 300;
     transform.position = pos;
 }
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 Cherno · Jun 27, 2015 at 01:02 AM 1
Share

This solution is better than $$anonymous$$e since it only changes the transform.position if the x values is outside the limits. +1

avatar image
1

Answer by Cherno · Jun 27, 2015 at 12:27 AM

You either use a collider around the level, or do what you already tried, but with the correct logic. It is true that you can't directly change single values of a transform. However, you can pass it a complete Vector3:

 void Update() {
      //declare a temporary variable that holds the current position
      Vector3 playerPos = transform.position;
      //clamp the temporary variable's x value
      playerPos.x = Mathf.Clamp(-300,300);
      //assign the complete temp variable back to the transform
      transform.position = playerPos;
 
      
 
 }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2D Sidescroller Issues Please help. 1 Answer

Changing player's moving direction 0 Answers

OnMouseDrag issue, Event System 0 Answers

How to run a script every time a scene is loaded. 0 Answers

How to make a script for "jetpack" boost animation 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