- Home /
Camera Controls
Hi all i'm new to development and programming/scripting, and having a few problems. I want to create a simple script for camera movement (left,right, forward, back, rotate and zoom in & out). I've been looking through the question/answers here and the internet for some guidance and have come up with the following script.
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
int cameraVelocity = 10;
// Use this for initialization
void Start () {
// Initial position set to current active player
}
// Update is called once per frame
void update () {
// Camera Movement - Forward, Back, Left, Right, Rotate, Up & Down
if((Input.GetKey(KeyCode.W))){
transform.Translate((Vector3.forward* cameraVelocity) * Time.deltaTime);
}
if((Input.GetKey(KeyCode.S))){
transform.Translate((Vector3.back* cameraVelocity) * Time.deltaTime);
}
if((Input.GetKey(KeyCode.A))){
transform.Translate((Vector3.left* cameraVelocity) * Time.deltaTime);
}
if((Input.GetKey(KeyCode.D))){
transform.Translate((Vector3.right * cameraVelocity) * Time.deltaTime);
}
// Will change for a zoom on Mouse wheel later
//if((Input.GetKey(KeyCode.UpArrow))){
// transform.Translate((Vector3.up * cameraVelocity) * Time.deltaTime);
//}
//if((Input.GetKey(KeyCode.DownArrow))){
// transform.Translate((Vector3.down * cameraVelocity) * Time.deltaTime);
//}
// Rotate Clockwise
//if(Input.GetKey(KeyCode.Q)){
// transform.Rotate((0,90,0 * cameraVelocity) * Time.deltaTime);
//}
// Rotate Anti-Clockwise
//if(Input.GetKeyDown(KeyCode.E)){
// transform.Rotate((0,-90,0 * cameraVelocity) * Time.deltaTime);
//}
}
}
This has been attached the MainCamera object, i have currently commented out the up/down & rotate as even the WASD has stopped working, but i've never had the rotate command working. Can anyone assist me on what i'm doing wrong?
At a later date i would like to add commands that will centre the camera behind different player objects with the key presses 1-4 and lock the height of the camera on the Y Axis but first really want to get the standard controls working.
Answer by robertbu · Aug 19, 2014 at 03:58 PM
Your big problem is that you've misspelled 'Update()' You have 'update' with a lower case 'u' and it needs to be 'Update' with an upper case 'U'. As for your rotation code, you must pass transform.Rotate a Vector3. You have some numbers and commas there, but you must create a new Vector3 out of them. Here is your code with the two fixes:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
float cameraVelocity = 10.0f;
float rotationSpeed = 1.0f;
// Use this for initialization
void Start () {
// Initial position set to current active player
}
// Update is called once per frame
void Update () {
// Camera Movement - Forward, Back, Left, Right, Rotate, Up & Down
if((Input.GetKey(KeyCode.W))){
transform.Translate((Vector3.forward* cameraVelocity) * Time.deltaTime);
}
if((Input.GetKey(KeyCode.S))){
transform.Translate((Vector3.back* cameraVelocity) * Time.deltaTime);
}
if((Input.GetKey(KeyCode.A))){
transform.Translate((Vector3.left* cameraVelocity) * Time.deltaTime);
}
if((Input.GetKey(KeyCode.D))){
transform.Translate((Vector3.right * cameraVelocity) * Time.deltaTime);
}
//Will change for a zoom on Mouse wheel later
if((Input.GetKey(KeyCode.UpArrow))){
transform.Translate((Vector3.up * cameraVelocity) * Time.deltaTime);
}
if((Input.GetKey(KeyCode.DownArrow))){
transform.Translate((Vector3.down * cameraVelocity) * Time.deltaTime);
}
//Rotate Clockwise
if(Input.GetKey(KeyCode.Q)){
transform.Rotate(new Vector3(0,90,0) * rotationSpeed * Time.deltaTime);
}
//Rotate Anti-Clockwise
if(Input.GetKey(KeyCode.E)){
transform.Rotate(new Vector3(0,-90,0) * rotationSpeed * Time.deltaTime);
}
}
}
I made a couple of other small changes. I made 'cameraVelocity' a float, added a 'rotationSpeed', and change GetKeyDown to GetKey.
Thanks that's great, completely missed the lowercase "U" in Update.
Answer by Pecek · Aug 19, 2014 at 04:12 PM
First of all, you shouldn't hardcode the control keys like that, if you use "GetButton" instead of "GetKey" the player can customize it(and it won't give you nightmares if a few weeks later you want to change the controls a bit - if you ever had a game with more than 4 buttons you know what I'm saying).
I don't know c#, but I guess you have to explicitly tell that you want to create a Vector3, I just remade your code in boo(the rotating part) and it works fine.
def Update ():
if Input.GetButtonDown("Horizontal"):
transform.Rotate(Vector3(0, 90, 0) * 10 * Time.deltaTime)
Btw if you are using exactly this code it shouldn't work at all since you are doing the movement in the update function instead of Update.
Thanks for the reply, i've done a little investigation and trial and error with using GetButton & GetButtonDown. But can not get it working. If i use either of the above commands the camera only moves in one direction.
if (Input.GetButton("Vertical")){
transform.Translate (Vector3.forward * cameraSpeed * Time.deltaTime);
}
if(Input.GetButton("Horizontal")){
transform.Translate (Vector3.left * cameraSpeed * Time.deltaTime);
}
// Rotate Camera Clockwise & Anti-Clockwise
if(Input.GetButton("Rotate")){
transform.Rotate(new Vector3(0,90,0) * cameraRotSpeed * Time.deltaTime);
}
From what i've read on the forums and others everyone seems to be saying the GetButton command will pickup the Pos & Neg inputs and work in both directions, but i'm only going forward, left and clockwise, with either key. Am i missing something????