- Home /
Move the camera then the cursor is at the screen edge
HI! I want to make a script that moves the camera then your mouse cursor is at the end of your screen as in strategy games. Have made a script with the camera follow the mouse but the problem is it move the camera then it is on the screen and not centred and it will only move on in two directions, left and right.
Answer by AlucardJay · Jun 27, 2012 at 03:17 PM
Check my answer on this question :
http://answers.unity3d.com/questions/266454/top-down-2d-gamehow-to-make-the-camera-move-on-hit.html
EDIT :
the mouse inputs are X and Y, what you change from those values is your choice. For the camera, use mouse X for camera X, and mouse Y for camera Z. Attach this script to the camera :
#pragma strict
public var Boundary : int = 50; // distance from edge scrolling starts
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; // move on +X axis
}
if (Input.mousePosition.x < 0 + Boundary)
{
transform.position.x -= speed * Time.deltaTime; // move on -X axis
}
if (Input.mousePosition.y > theScreenHeight - Boundary)
{
transform.position.z += speed * Time.deltaTime; // move on +Z axis
}
if (Input.mousePosition.y < 0 + Boundary)
{
transform.position.z -= speed * Time.deltaTime; // move on -Z axis
}
}
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 );
}
Thanks this solved all my problems with it except one I needed too change y to z to get the camera to move on the direction I wanted but now it's move on -z axel even if I haven't the mouse at the screen edge, what is did I wrong then I only changed the y to z?
I have updated the answer. Hopefully this is what you want.
$$anonymous$$ouse Input is X and Y axis (check the numbers showing OnGUI), so use mouseX on cameraX, and mouseY on cameraZ.
Answer by Flufflesthepancake · Jun 27, 2012 at 01:05 PM
Using my basic knowledge and question answering amazingness (it's pretty poor):
take the mouse position from a raycast: http://unity3d.com/support/documentation/ScriptReference/Input-mousePosition.html
and then make it so that if it's a certain distance away from the edges (determine with Screen.width and Screen.height) you perform the necessary camera movement (seeing as you haven't explained how the camera moves, whether that's a rotation or a pan or a track)
You'd have to iron out things to make it pretty, but that'd be the basic idea.
Your answer