- Home /
Camera movement according to rotation and actual position RTS
Hi guys i'm doing and RTS TD game. My camera can rotate all around the field. My camera has a rotation on X of 65 degrees from start; the player can see the whole field from that view. Since my player can rotate at will, i want to let him move up, down, left and right after he rotated, and my plan is to keep W A S D keys to move up, left, down from the new point of view. My issue is W and S in Space.World won't let the player move after rotating his camera (it ignores his actual rotatio), so i tried using Space.Self and Vector3.forward, Vector3.up and Vector3.back, Vector3.down but all of them won't have the results i want, because .forward and .back makes the player zoom in and out respectively. And .up and .down actually kinda work, but they make the player zoom in and out too.
So any ideas to let the player keep his WASD movement after used the rotatios feature?
Right now i letft W and S on Space.World, so i can move around without rotation. And A and D can move perfectly before and after rotating.
using UnityEngine;
public class CameraController : MonoBehaviour {
public float panSpeed = 30f;
public float panBorderThickness = 10f;
public float scrollSpeed = 20f;
public Vector2 panLimit;
public float minY;
public float maxY;
Vector3 lastMousePosition;
float rotateSpeed = 5f;
void Update()
{
if (GameManager.GameIsOver)
{
this.enabled = false;
return;
}
if (Input.GetKey("w") || Input.mousePosition.y >= Screen.height - panBorderThickness)
{
transform.Translate(Vector3.forward * panSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey("s") || Input.mousePosition.y <= panBorderThickness)
{
transform.Translate(Vector3.back * panSpeed * Time.deltaTime, Space.World);
}
if (Input.GetKey("d") || Input.mousePosition.x >= Screen.width - panBorderThickness)
{
transform.Translate(Vector3.right * panSpeed * Time.deltaTime, Space.Self);
}
if (Input.GetKey("a") || Input.mousePosition.x <= panBorderThickness)
{
transform.Translate(Vector3.left * panSpeed * Time.deltaTime, Space.Self);
}
if (Input.GetKey("q"))
{
transform.Rotate(Vector3.up * Time.deltaTime * -scrollSpeed*4, Space.World);
}
if (Input.GetKey("e"))
{
transform.Rotate(Vector3.down * Time.deltaTime * -scrollSpeed*4, Space.World);
}
// Middle click mouse rotation
if (Input.GetMouseButton(2))
{
// if the game window is separate from the editor window and the editor
// window is active then you go to right-click on the game window the
// rotation jumps if we don't ignore the mouseDelta for that frame.
Vector3 mouseDelta;
if (lastMousePosition.x >= 0
&& lastMousePosition.y >= 0
&& lastMousePosition.x <= Screen.width
&& lastMousePosition.y <= Screen.height)
mouseDelta = Input.mousePosition - lastMousePosition;
else
mouseDelta = Vector3.zero;
Vector3 rotation = Vector3.up * Time.deltaTime * rotateSpeed * mouseDelta.x;
rotation += Vector3.left * Time.deltaTime * rotateSpeed * mouseDelta.y;
transform.Rotate(rotation, Space.Self);
// Make sure z rotation stays locked
rotation = transform.rotation.eulerAngles;
rotation.z = 0;
transform.rotation = Quaternion.Euler(rotation);
}
lastMousePosition = Input.mousePosition;
Vector3 pos = transform.position;
pos.x = Mathf.Clamp(pos.x, -panLimit.x, panLimit.x + 90);
pos.z = Mathf.Clamp(pos.z, -panLimit.y, panLimit.y);
pos.y = Mathf.Clamp(pos.y, minY, maxY);
float scroll = Input.GetAxis("Mouse ScrollWheel");
pos.y -= scroll * 100 * scrollSpeed * Time.deltaTime;
transform.position = pos;
}
}
Answer by DreadKyller · Dec 23, 2017 at 10:05 PM
Instead of using the directions provided by Vector3 ( Vector3.up
, Vector3.left
, etc) use the up, right, and forward properties of the transform ( transform.forward
, transform.up
, transform.right
) to get backwads dow and left just negate it:
Forward = transform.forward
Backwards = -transform.forward
Up = transform.up
Down = -transform.up
Right = transform.right
Left = -transform.right
These directions represent the local direction vectors for the transform, in world space.