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
2
Question by Frede1012 · Aug 06, 2013 at 09:10 PM · c#spriteobjectvector

Limit a sprite to not go off-screen

Hello there!

I would like to limit my sprite object so it can't go off-screen. The object is controlled by mouse so it is possible to get some of the object out of camera view.

How would i solve this?

EDIT: I have now done this, but it only "stops" at the center of the sprite, so half the sprite can still go off-screen.

 Vector3 viewPos = Camera.main.WorldToViewportPoint (this.transform.position);
 viewPos.x = Mathf.Clamp01 (viewPos.x);
 viewPos.y = Mathf.Clamp01 (viewPos.y);
 this.transform.position = Camera.main.ViewportToWorldPoint (viewPos);

Thanks!

  • Frederik

Comment
Add comment · Show 8
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 perchik · Aug 06, 2013 at 09:12 PM 0
Share

Do you want the whole piece to stay on the screen or is it okay for parts of it go off the screen

avatar image Frede1012 · Aug 06, 2013 at 09:13 PM 0
Share

I don't want any part of the object to be able to go off-screen.

avatar image Frede1012 · Aug 07, 2013 at 10:16 PM 0
Share

Updated OP with new code

avatar image perchik · Aug 07, 2013 at 10:30 PM 0
Share

How are you moving it with the mouse?

avatar image Frede1012 · Aug 07, 2013 at 10:36 PM 0
Share
 Vector3 mousePos = Input.mousePosition;
 mousePos.z = 1.0F;
 Vector2 worldPos = Camera.main.ScreenToWorldPoint (mousePos);
 this.transform.position = worldPos;
Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by ildac · Nov 23, 2013 at 10:26 AM

I know it's an old question, but since I've started now playing around with Unity3D 4.3, i had the same problem and i want to share with you my solution to understand if I'm doing it right.

I've managed to keep the full sprite in the screen with these code:

         Vector3 playerSize = renderer.bounds.size;
 
         // Here is the definition of the boundary in world point
         var distance = (transform.position - Camera.main.transform.position).z;
 
         var leftBorder = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distance)).x + (playerSize.x/2);
         var rightBorder = Camera.main.ViewportToWorldPoint (new Vector3 (1, 0, distance)).x - (playerSize.x/2);
 
         var bottomBorder = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distance)).y + (playerSize.y/2);
         var topBorder = Camera.main.ViewportToWorldPoint (new Vector3 (0, 1, distance)).y - (playerSize.y/2);
     
         // Here the position of the player is clamped into the boundaries
         transform.position = (new Vector3 (
             Mathf.Clamp (transform.position.x, leftBorder, rightBorder),
             Mathf.Clamp (transform.position.y, bottomBorder, topBorder),
             transform.position.z)
         );

What it does is simply reduce the camera view of half of the sprite size, in this way when the center of the sprite hits the boundary it not outside the view but just inside.

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 booosh · Dec 26, 2013 at 01:06 AM 0
Share

This is a great answer, thank you.

avatar image
2

Answer by perchik · Aug 07, 2013 at 11:01 PM

You could use your current code, but instead of clamping x and y from 0 to 1, clamp it to a smaller range.

 float widthRel = width / (Screen.width); //relative width
 float heightRel= height /(Screen.height); //relative height
 
 Vector3 viewPos = Camera.main.WorldToViewportPoint (this.transform.position);
 viewPos.x = Mathf.Clamp(viewPos.x, widthRel, 1-widthRel);
 viewPos.y = Mathf.Clamp(viewPos.y, heightRel, 1-heightRel);
 this.transform.position = Camera.main.ViewportToWorldPoint (viewPos);

Your code works because Clamp01 returns a number between 0 and 1, and the viewport starts at (0,0) and goes to (1,1). My code calculates the relative width of the object, and clamps the valid region to a subset of the screen.

If this doesn't work exactly right, you might need to change the clamps to Mathf.Clamp(viewPos.x, widthRel /2 , 1-(widthRel/2)) because the center of the object is what moves.

Comment
Add comment · Show 5 · 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 Frede1012 · Aug 07, 2013 at 11:16 PM 0
Share

float widthRel = width / (Screen.width); //relative width float heightRel= height /(Screen.height); //relative height

What are these for? It isn't a variable?

avatar image perchik · Aug 07, 2013 at 11:17 PM 0
Share

thats whatever your sprite width and height is (you have to either replace width and height with whatever your sprite's width and height is, or write some script to check out that objects bounds to figure out what its width and height is)

avatar image Frede1012 · Aug 07, 2013 at 11:22 PM 0
Share

Can i use the unity object scale?

avatar image robertbu · Aug 07, 2013 at 11:31 PM 0
Share

Two issues with the code here. Both are fixable. First, unless you've been very careful to create your scene as pixel perfect, the amount of screen pixels the object takes up will not match the pixel size of the texture. Second, your widthRel and heightRel are twice too big. You need to divide the numbers by two.

As for calculating the size, the first thing to figure out is the world size of your object. That will depend on what plane you are using to display your sprites. The build in plane has a size of 10 x 10 with a scale of (1,1,1). If that is what you are using, I'd switch and use a plane made with the CreatePlane editor script. You can make it a vertical orientation and size of 1 x 1. Then transform.localScale will contain the world width and height.

With the world width and height, you can do a WorldToViewportPoint() on the center point and then one of the corners. Finding the distance between the two will give you the 'widthRe' and 'heightRel' to use in the code above.

avatar image Frede1012 · Aug 08, 2013 at 10:37 AM 0
Share
 float widthRel = this.transform.localScale.y / (Screen.width) / 2; //relative width
  float heightRel = this.transform.localScale.x / (Screen.height) / 2; //relative height
      
 Vector3 viewPos = Camera.main.WorldToViewportPoint (this.transform.position);
 viewPos.x = $$anonymous$$athf.Clamp (viewPos.x, widthRel, 1 - widthRel);
 viewPos.y = $$anonymous$$athf.Clamp (viewPos.y, heightRel, 1 - heightRel);
 this.transform.position = Camera.main.ViewportToWorldPoint (viewPos);

This is close, though not perfect. How would i calculate the width & height of the sprite object? (Since localScale isnt accurate enough)

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

19 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

Related Questions

Multiple Cars not working 1 Answer

Show far away objects as a circle (big dot) 2 Answers

How to know from which direction there is a collision with an object? in C# 1 Answer

Distribute terrain in zones 3 Answers

Multidimensional array of objects in C# 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