- Home /
RTS Camera not working as expected
I'm having a problem with the RTS camera I'm making:
It's supposed to offer rotation using q/e and then move based upon the rotation.
However, it's only moving along one axis and the angle isn't working at all.
using UnityEngine;
using System.Collections;
public class MovementCamera : MonoBehaviour {
#region runtimevariables
private float mouseX;
private float mouseY;
private int screenWidth;
private int screenHeight;
private Vector2 cameraPosition;
public float Angle;
#endregion
// Settings
public int ScrollDistance = 100;
public float MovementSpeed = 5;
// Initalize
void Start() {
cameraPosition = new Vector2();
Angle = 0;
}
// TODO:
// --> Smooth dragcamera
// --> Add height based upon wheel (sin-curve)
void LateUpdate () {
mouseX = Input.mousePosition.x;
mouseY = Input.mousePosition.y;
screenWidth = Screen.width;
screenHeight = Screen.height;
float xRate = 0.0f;
float yRate = 0.0f;
if (ScrollDistance >= mouseX)
xRate = -(1 - (mouseX / ScrollDistance));
else if (mouseX >= screenWidth - ScrollDistance)
xRate = (mouseX - screenWidth + ScrollDistance) / ScrollDistance;
if (ScrollDistance >= mouseY)
yRate = -(1 - (mouseY / ScrollDistance));
else if (mouseY >= screenHeight - ScrollDistance)
yRate = (mouseY - screenHeight + ScrollDistance) / ScrollDistance;
float yMovement = Mathf.Min (yRate + Input.GetAxis ("Vertical"), 1) * Time.deltaTime * MovementSpeed;
float xMovement = Mathf.Min (xRate + Input.GetAxis ("Horizontal"), 1) * Time.deltaTime * MovementSpeed;
Angle += ((Input.GetKey (KeyCode.Q) ? 1 : 0) - (Input.GetKey (KeyCode.E) ? 1 : 0) * Time.deltaTime);
Vector3 newPosition = new Vector3(
cameraPosition.x + xMovement * Mathf.Cos (Angle * Mathf.Deg2Rad) - yMovement * Mathf.Sin (Angle * Mathf.Deg2Rad),
5,
cameraPosition.y + xMovement * Mathf.Sin (Angle * Mathf.Deg2Rad) + yMovement * Mathf.Cos (Angle * Mathf.Deg2Rad)
);
cameraPosition.x = newPosition.x;
cameraPosition.y = newPosition.y;
transform.position = newPosition;
}
}
Comment
Best Answer
Answer by Vox Nephila · Jun 07, 2013 at 05:02 AM
You're going to want to look into Transform.eulerAngles in order to get proper angles. Not to mention you're just setting the position, which will never rotate the object no matter how hard you try.