• 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
1
Question by hilchev · Jun 13, 2012 at 10:16 PM · camera2dmousescreentopdown

Top-Down 2D game:How to make the camera move on hitting the sides of the screen?

Hi guys! I'm trying to make a topdown 2D game so that I can learn Unity :) So far i've made the character, the map and some other stuff but i'm having trouble with the camera. How can i make the camera move only when the player moves the mouse to the sides of the screen, without the unit chasing/moving/rotating towards the mouse?

P.S.:I tried finding other questions or close functions but i didn't find a lot of useful stuff/info.

Comment
Add comment · Show 2
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 AlucardJay · Jun 14, 2012 at 01:11 AM 1
Share

you need to check if the mouse position is near one of the edges, then affect your character accordingly. I am on my way out, but shall check this qn later. In the meantime, have a look at Input.mousePosition , Screen.height and width :

http://unity3d.com/support/documentation/ScriptReference/Input-mousePosition.html

http://unity3d.com/support/documentation/ScriptReference/Screen-height.html

http://unity3d.com/support/documentation/ScriptReference/Screen-width.html

e.g.

  if (Input.mousePosition.x > Screen.width - 50) [move char right];
  if (Input.mousePosition.x < 0 + 50) [move char left];
  if (Input.mousePosition.y > Screen.height - 50) [move char down];
  if (Input.mousePosition.y < 0 + 50) [move char up];
avatar image hilchev · Jun 14, 2012 at 09:05 PM 0
Share

I'm gonna experiment a little with those, but still, please do give an idea if you have one :) Тhank you for pointing me in the right direction.

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by AlucardJay · Jun 15, 2012 at 02:20 PM

Expanding on the comment : you need to check if the mouse position is wit$$anonymous$$n a certain distance from an edge, then affect your character accordingly. Have a look at Input.mousePosition , Screen.height and width :

http://unity3d.com/support/documentation/ScriptReference/Input-mousePosition.html

http://unity3d.com/support/documentation/ScriptReference/Screen-height.html

http://unity3d.com/support/documentation/ScriptReference/Screen-width.html

: Create a New Scene, then Create a Cube. Attach t$$anonymous$$s Script to the Cube and $$anonymous$$t Play.

 #pragma strict
 
 public var Boundary : int = 50;
 public var speed : int = 5;
 
 private var theScreenWidth : int;
 private var theScreenHeight : int;
 
 function Start() 
 {
     theScreenWidth = Screen.width;
     theScreenHeight = Screen.height;
 }
 
 function Update() 
 {
     if (Input.mousePosition.x > theScreenWidth - Boundary)
     {
         transform.position.x += speed * Time.deltaTime;
     }
     
     if (Input.mousePosition.x < 0 + Boundary)
     {
         transform.position.x -= speed * Time.deltaTime;
     }
     
     if (Input.mousePosition.y > theScreenHeight - Boundary)
     {
         transform.position.y += speed * Time.deltaTime;
     }
     
     if (Input.mousePosition.y < 0 + Boundary)
     {
         transform.position.y -= speed * Time.deltaTime;
     }
     
 }    
 
 function OnGUI() 
 {
     GUI.Box( Rect( (Screen.width / 2) - 140, 5, 280, 25 ), "Mouse Position = " + Input.mousePosition );
     GUI.Box( Rect( (Screen.width / 2) - 70, Screen.height - 30, 140, 25 ), "Mouse X = " + Input.mousePosition.x );
     GUI.Box( Rect( 5, (Screen.height / 2) - 12, 140, 25 ), "Mouse Y = " + Input.mousePosition.y );
 }


Comment
Add comment · Show 2 · 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 hilchev · Jun 16, 2012 at 12:26 PM 0
Share

Thakn you both, now I know how this works :)

avatar image AlucardJay · Jun 16, 2012 at 03:17 PM 0
Share

no problem (am same person btw =]). I forgot to include some GUI display stuff so you can see the mouse position relative to the screen. Add this to the end of the script :

 function OnGUI() 
 {
     GUI.Box( Rect( (Screen.width / 2) - 140, 5, 280, 25 ), "Mouse Position = " + Input.mousePosition );
     GUI.Box( Rect( (Screen.width / 2) - 70, Screen.height - 30, 140, 25 ), "Mouse X = " + Input.mousePosition.x );
     GUI.Box( Rect( 5, (Screen.height / 2) - 12, 140, 25 ), "Mouse Y = " + Input.mousePosition.y );
 }
avatar image
0

Answer by solidd_swa · Sep 08, 2013 at 03:36 PM

there is a plugin called Univerture, check it out

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

rotate camera on mouse reaching edges 1 Answer

Camera looking down 1 Answer

Question about sending physics raycast from the camera 1 Answer

2D camera zoom smoothing and limitations? 1 Answer

Automatically adjusting the camera view 0 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