I’ve been trying to create a very simple camera script for an RTS, and I have some questions.
What I curently have is this:
var cameraObject : GameObject;
var speed : float = 50;
function Update () {
// Horizontal camera movement
if(Input.mousePosition.x == 0){
cameraObject.transform.Translate(Vector3.left * speed * Time.deltaTime);
}
if(Input.mousePosition.x == Screen.width){
cameraObject.transform.Translate(Vector3.right * speed * Time.deltaTime);
}
// Vertical camera movement
if(Input.mousePosition.y == 0){
cameraObject.transform.Translate(Vector3.down * speed * Time.deltaTime);
}
if(Input.mousePosition.y == Screen.height){
cameraObject.transform.Translate(Vector3.up * speed * Time.deltaTime);
}
}
Now, obviously, is I put the mouse on the bottom of the screen, the camera moves to the ground, which is a flaw, it should only move on a “plane” (I hope you get what I mean). It should not be moving closer or further away from the ground.
So my question is, what do I need to write in the "if(mousPosition.y == 0){} statement, to make this work?
Og and by the way, the if-statements that are supposed to detect if the mouse is at the very end of the screen (Screen.height/width) are not working for some reason… Any sugestions?
Hello, I had the same problem and I found a solution, just put the camera in an empty gameObject.
The cam has a rotation but not the empty game object, so, it will be fine. (cameraObject → the empty gameObject)
Sorry for my english, I’m french.
P.S: I have created a script for controlling the cam with the arrows, there it is:
var speed = 20.0;
function Update ()
{
var x : Vector3 = Input.GetAxis("Horizontal") * transform.right * speed * Time.deltaTime;
var z : Vector3 = Input.GetAxis("Vertical") * transform.forward * speed * Time.deltaTime;
transform.Translate(x + z);
}
#pragma strict
var speedArrow = 20.0;
var speedMouse = 20.0;
var cameraObject : GameObject;
function Update ()
{
// camera movement with arrows(Axis)
var x : Vector3 = Input.GetAxis("Horizontal") * transform.right * speedArrow * Time.deltaTime;
var z : Vector3 = Input.GetAxis("Vertical") * transform.forward * speedArrow * Time.deltaTime;
transform.Translate(x + z);
// Horizontal camera movement
if(Input.mousePosition.x == 0){
cameraObject.transform.Translate(-Vector3.right * speedMouse * Time.deltaTime);
}
if(Input.mousePosition.x == Screen.width){
cameraObject.transform.Translate(Vector3.right * speedMouse * Time.deltaTime);
}
// Vertical camera movement
if(Input.mousePosition.y == 0){
cameraObject.transform.Translate(-Vector3.forward * speedMouse * Time.deltaTime);
}
if(Input.mousePosition.y == Screen.height){
cameraObject.transform.Translate(Vector3.forward * speedMouse * Time.deltaTime);
}
}
Everything is working but I think that it could be improved if the area of the mouse on the borders of the screen was bigger (it’s too small). But I don’t know how to do this. Maybe with triggers attached to the empty gameObject that contains the camera?
Well it works now… The only problem is that when I go to full screen mode, the program doesn’t seem ot catch when my mouse is at the extreme points of the screen… (e.g. Screen.width)… Which is a problem.
Here’s the finished camera script for an RTS game, use it if you want:
/*
This script is attached to the camera object in your scene.
It makes it possible to move the camera by moving your curser
to the ends of the screen, and it workds for all resolutions.
*/
var cameraObject : GameObject;
var speed : float = 50;
function Update () {
// Horizontal camera movement
if(Input.mousePosition.x < 10){
cameraObject.transform.Translate(Vector3.left * speed * Time.deltaTime);
}
if(Input.mousePosition.x > Screen.width-10){
cameraObject.transform.Translate(Vector3.right * speed * Time.deltaTime);
}
// Vertical camera movement
if(Input.mousePosition.y < 10){
cameraObject.transform.Translate(Vector3.back * speed * Time.deltaTime);
}
if(Input.mousePosition.y > Screen.height-10){
cameraObject.transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
}
Remember, for this to work, you must put your Main Camera inside an empty Game Object, and attach this script to THAT object, not the camera itself. Also, this game object must be associated with the “cameraObject” variable in the inspector!