RTS camera Controles
I got the basics made of an RTS camera it can move, zoom and rotate. when I rotate the camera in the other direction my camera controls become inverted the up arrow key sends it backward and vice versa
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tcam : MonoBehaviour {
//direction
public float speed = 7f;
public float maxZoom = 5f;
public float minZoom = 30f;
public float curentZoom = 25f;
//rotation
public float minX = -360.0f;
public float maxX = 360.0f;
public float minY = -45.0f;
public float maxY = 45.0f;
public float sensX = 100.0f;
public float sensY = 100.0f;
float rotationY = 0.0f;
float rotationX = 0.0f;
public float mSpeed = 7f;
// Use this for initialization
void Start() {
curentZoom = GetComponent<Transform>().position.y;
}
// Update is called once per frame
void Update() {
//basic movment
if (Input.GetKey(KeyCode.RightArrow))
{
transform.position += Vector3.right * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.position += Vector3.left * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.UpArrow))
{
transform.position += Vector3.forward * speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.DownArrow))
{
transform.position += Vector3.back * speed * Time.deltaTime;
}
//zoom
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
//zoom in
if (curentZoom > maxZoom)
{
GetComponent<Transform>().position = new Vector3(transform.position.x, transform.position.y - .3f, transform.position.z + .1f);
curentZoom = GetComponent<Transform>().position.y;
}
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
//zoom out
if (curentZoom < minZoom)
{
GetComponent<Transform>().position = new Vector3(transform.position.x, transform.position.y + .3f, transform.position.z - .1f);
curentZoom = GetComponent<Transform>().position.y;
}
}
//rotation
if (Input.GetMouseButton(0))
{
rotationX += Input.GetAxis("Mouse X") * sensX * Time.deltaTime;
rotationY += Input.GetAxis("Mouse Y") * sensY * Time.deltaTime;
rotationY = Mathf.Clamp(rotationY, minY, maxY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
}
}
Comment