- Home /
RTS Camera Rotation and Movement
Hello. I am new to scripting as well as c# and so far I have been going along with the Unity API and figuring things out by googling questions, but I can't seem to find an answer for this. I am making a RTS style camera, and I want to be able to rotate the camera's view with right click + drag, which I have managed to do with line 170 -> 175, but I can't get the direction the camera is heading when W is pressed to rotate along with it, and when I right click it seems the camera reverts to 0 on the X and Z axis rotation (the camera starts out looking at the ground). The script is attached to the camera itself and the camera is not attached to anything else. Any information you can help me with would be excellent. Thanks.
using UnityEngine;
using System.Collections;
public class RTSCam01 : MonoBehaviour
{
//Map border constraints so the camera doesn't go off the map area//
public float mapBorderForward = 37f;
public float mapBorderBack = 50f;
public float mapBorderLeft = 38f;
public float mapBorderRight = 38f;
//Screen constraints for edge scrolling//
private int theScreenWidth = Screen.width;
private int theScreenHeight = Screen.height;
//Camera zooming constraints//
public float cameraSpeed = 10f;
public float cameraZoomSpeed = 30f;
public float cameraZoomIn = 4f;
public float cameraZoomOut = 10f;
//Camera rotation constraints//
private float mouseX;
private float mouseY;
public float cameraRotateSpeed = 1f;
public float cameraVertRotateMin = 0f;
public float cameraVertRotateMax = 40f;
void Start()
{
}
// Update is called once per frame
void Update ()
{
//Mouse edge scroll forward//
if (Input.mousePosition.y >= theScreenHeight * 0.98f)
{
transform.Translate(Vector3.forward * Time.deltaTime * cameraSpeed, Space.World);
if (transform.position.z > mapBorderForward)
{
Vector3 clampForward = transform.position;
clampForward.z = mapBorderForward;
transform.position = clampForward;
}
//Debug.Log (transform.position);
}
//Mouse edge scroll back//
if (Input.mousePosition.y <= theScreenHeight * 0.02f)
{
transform.Translate(Vector3.back * Time.deltaTime * cameraSpeed, Space.World);
if (transform.position.z < -mapBorderBack)
{
Vector3 clampBack = transform.position;
clampBack.z = -mapBorderBack;
transform.position = clampBack;
}
//Debug.Log(transform.position);
}
//Mouse edge scroll left//
if (Input.mousePosition.x <= theScreenWidth * 0.02f)
{
transform.Translate(Vector3.left * Time.deltaTime * cameraSpeed, Space.World);
if (transform.position.x < -mapBorderLeft)
{
Vector3 clampLeft = transform.position;
clampLeft.x = -mapBorderLeft;
transform.position = clampLeft;
}
//Debug.Log(transform.position);
}
//Mouse edge scroll right//
if (Input.mousePosition.x >= theScreenWidth * 0.98f)
{
transform.Translate(Vector3.right * Time.deltaTime * cameraSpeed, Space.World);
if (transform.position.x > mapBorderRight)
{
Vector3 clampRight = transform.position;
clampRight.x = mapBorderRight;
transform.position = clampRight;
}
//Debug.Log(transform.position);
}
//W forward key//
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.forward * Time.deltaTime * cameraSpeed, Space.World);
if (transform.position.z > mapBorderForward)
{
Vector3 clampForwardKey = transform.position;
clampForwardKey.z = mapBorderForward;
transform.position = clampForwardKey;
}
//Debug.Log (transform.position);
}
//S back key//
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.back * Time.deltaTime * cameraSpeed, Space.World);
if (transform.position.z < -mapBorderBack)
{
Vector3 clampBackKey = transform.position;
clampBackKey.z = -mapBorderBack;
transform.position = clampBackKey;
}
//Debug.Log(transform.position);
}
//A left key//
if (Input.GetKey(KeyCode.A))
{
transform.Translate(Vector3.left * Time.deltaTime * cameraSpeed, Space.World);
if (transform.position.x < -mapBorderLeft)
{
Vector3 clampLeftKey = transform.position;
clampLeftKey.x = -mapBorderLeft;
transform.position = clampLeftKey;
}
//Debug.Log(transform.position);
}
//D right key//
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector3.right * Time.deltaTime * cameraSpeed, Space.World);
if (transform.position.x > mapBorderRight)
{
Vector3 clampRightKey = transform.position;
clampRightKey.x = mapBorderRight;
transform.position = clampRightKey;
}
//Debug.Log(transform.position);
}
//Mouse wheel scroll in//
if(Input.GetAxis("Mouse ScrollWheel") > 0)
{
Vector3 camDown = new Vector3(0, -1, 0);
transform.Translate(camDown * cameraZoomSpeed * Time.deltaTime, Space.World);
if (transform.position.y < cameraZoomIn)
{
Vector3 camZoomInMax = transform.position;
camZoomInMax.y = cameraZoomIn;
transform.position = camZoomInMax;
}
Debug.Log(transform.position);
}
//Mouse wheel scroll out//
if(Input.GetAxis("Mouse ScrollWheel") < 0)
{
Vector3 camUp = new Vector3(0, 1, 0);
transform.Translate(camUp * cameraZoomSpeed * Time.deltaTime, Space.World);
if (transform.position.y > cameraZoomOut)
{
Vector3 camZoomOutMax = transform.position;
camZoomOutMax.y = cameraZoomOut;
transform.position = camZoomOutMax;
}
Debug.Log(transform.position);
}
//Right click and drag mouse rotate//
if(Input.GetKey(KeyCode.Mouse1))
{
mouseX += Input.GetAxis("Mouse X");
mouseY -= Input.GetAxis("Mouse Y");
transform.eulerAngles = new Vector3(mouseY, mouseX, 0);
}
}
}
Comment