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 RecChemForbin · Nov 15, 2011 at 06:35 AM · c#transform

Trying to keep GameObject (Ship) locked into visible screen space

Hello again! I'm stuck on something that is driving me crazy!

I am doing a 2d side view game, with ship that I want to only to be able to travel to the edges of the screen and all space in between. When it reaches the top/bottom/sides of the screen I wish it to stop transforming in that direction so it will not travel off screen. I am using GetAxis to return the 1/-1 and with my current code my ship will move. It moves fine as long as I do not reach the edges, but when the screed edge is reached it will no longer move.

I do realize what my problem is from my code below:

 //Ship movement
     float verticalMovement = Input.GetAxis("Vertical") * moveSpeed;
     
     if(ship.transform.position.y <= 6 && ship.transform.position.y >= -4){
         ship.transform.Translate(0, (verticalMovement * Time.deltaTime), 0);
     }
     float horizontalMovement = Input.GetAxis("Horizontal") * moveSpeed;
     
     if(ship.transform.position.x <= 7.5f /*&& ship.transform.position.x >= -8.5f*/){
         ship.transform.Translate((horizontalMovement * Time.deltaTime),0,0);
     }


I'm using the if statement that checks for hard coded screen coordinates and when it reaches those coordinates it will no longer move up or down because the if statement returns a false since it reached the max coordinates and never reaches the transform line of code.

I'm not sure how to handle this since using getAxis returns the value for movement both up and down/left and right. If hardcoding the action keys I can of course do the "if" checks individually for all move directions, but do not want to hardcode these actions.

How can I check for max screen position, not move in that direction when max is reached and still move in the other directions so my ship will always remain on screen?

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 syclamoth · Nov 15, 2011 at 06:46 AM

Instead of flat preventing the player from moving, why don't you just cut off the bit that would move them over the edge?

 if(over the bottom edge)
 {
     horizontalMovement = Mathf.Clamp(horizontalMovement, 0, infinity);
 }
 
 blah blah move the ship here (outside the conditional statement)
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

How to instanciate transform to element of a list?,How Get Transform of a instanciated Prefap 3 Answers

Scale UI element On One Side 0 Answers

How can i get ONLY the childrens of a GameOnbject with GetComponentsInChildren method? 5 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