- Home /
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.
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];
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.
Answer by AlucardJay · Jun 15, 2012 at 02:20 PM
Expanding on the comment : you need to check if the mouse position is within 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 this Script to the Cube and hit 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 );
}
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 ), "$$anonymous$$ouse Position = " + Input.mousePosition );
GUI.Box( Rect( (Screen.width / 2) - 70, Screen.height - 30, 140, 25 ), "$$anonymous$$ouse X = " + Input.mousePosition.x );
GUI.Box( Rect( 5, (Screen.height / 2) - 12, 140, 25 ), "$$anonymous$$ouse Y = " + Input.mousePosition.y );
}