Hello Everyone,
I’m brand new to scripting and it has been both a struggle and a pleasure to learn. I have learned a lot by reading the scripting references and what i have read on this forum, but i am having an issue with my camera control. Everything works exactly how i want it except when i rotate the camera.
When i rotate the camera my camera moves get all messed up because the engine doesn’t reset the cameras axis after the rotation, so even though im dragging my mouse to the top of the screen it doesnt move the camera forward. It moves the camera forwards based off the world axis.
I know thats what im telling it to do with the “Space.World” input but the reason i need to use that so the engine doesnt move the Camera through the ground when i tell it to move it forward because i have the camera at a 45 degree angle from the top. Maybe i’m not clear in my explanation, but my question is:
Is there a way to tell it to reset the camera Axis while keeping the current rotation of the camera so when i rotate the camera the axis goes with it and i can then tell it to move along the Y,X,Z axis of the camera instead of the Space.world.
I really hope i explained this some what clear. Thanks all.
Here is source code for my Camera Control
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
//Camera Speed Variables
public float cameraSpeed = 30.0f;
public float shiftBonus = 90.0f;
//Mouse Screen Threshold
public float mouseEdgeThreshold = 15.0f;
public float mousePanEdgeThreshold = 0.0f;
//Camera Position Variables
public float camPositionLeftMax = -2000.0f;
public float camPositionRightMax = 2000.0f;
public float camPositionTopMax = 2000.0f;
public float camPositionBottomMax = -2000.0f;
public float camPositionX = 0.0f;
public float camPositionY = 0.0f;
public float PanSpeed = 1.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
camPositionX = Camera.main.transform.position.x;
camPositionY = Camera.main.transform.position.z;
//Camera Rotation Controls
if (Input.GetKey (KeyCode.Q)) {
transform.Rotate (0, 1, 0, Space.World);
}
if (Input.GetKey (KeyCode.E)) {
transform.Rotate (0, -1, 0, Space.World);
}
//Camera Horizontal Left Controls
if (camPositionX > camPositionLeftMax) {
if (Input.mousePosition.x < mouseEdgeThreshold) {
transform.Translate (Vector3.left * cameraSpeed, Space.World);
}
if ( Input.mousePosition.x < mouseEdgeThreshold Input.GetKey (KeyCode.LeftShift)) {
transform.Translate (Vector3.left * shiftBonus, Space.World);
}
if (Input.GetKey (KeyCode.A)) {
Camera.main.transform.Translate (Vector3.left * cameraSpeed, Space.World);
}
if (Input.GetKey (KeyCode.A) Input.GetKey (KeyCode.LeftShift)) {
transform.Translate (Vector3.left * shiftBonus, Space.World);
}
}
//Camera Horizontal Right Controls
if (camPositionX < camPositionRightMax) {
if (Input.mousePosition.x > Screen.width - mouseEdgeThreshold) {
transform.Translate (Vector3.right * cameraSpeed, Space.World);
}
if ( Input.mousePosition.x > Screen.width - mouseEdgeThreshold Input.GetKey (KeyCode.LeftShift)) {
transform.Translate (Vector3.right * shiftBonus, Space.World);
}
if (Input.GetKey (KeyCode.D)) {
transform.Translate (Vector3.right * cameraSpeed, Space.World);
}
if (Input.GetKey (KeyCode.D) Input.GetKey (KeyCode.LeftShift)) {
transform.Translate (Vector3.right * shiftBonus, Space.World);
}
}
//Camera Vertical Bottom Controls
if (camPositionY > camPositionBottomMax) {
if (Input.mousePosition.y < mouseEdgeThreshold) {
transform.Translate (Vector3.back * cameraSpeed, Space.World);
}
if ( Input.mousePosition.y < mouseEdgeThreshold Input.GetKey (KeyCode.LeftShift)) {
transform.Translate (Vector3.back * shiftBonus, Space.World);
}
if (Input.GetKey (KeyCode.S)) {
transform.Translate (Vector3.back * cameraSpeed, Space.World);
}
if (Input.GetKey (KeyCode.S) Input.GetKey (KeyCode.LeftShift)) {
transform.Translate (Vector3.back * shiftBonus, Space.World);
}
}
//Camera Vertical Top Controls
if (camPositionY < camPositionTopMax) {
if (Input.mousePosition.y > Screen.height - mouseEdgeThreshold) {
transform.Translate (Vector3.forward * cameraSpeed, Space.World);
}
if ( Input.mousePosition.y > Screen.height - mouseEdgeThreshold Input.GetKey (KeyCode.LeftShift)) {
transform.Translate (Vector3.forward * shiftBonus, Space.World);
}
if (Input.GetKey (KeyCode.W)) {
transform.Translate (Vector3.forward * cameraSpeed, Space.World);
}
if (Input.GetKey (KeyCode.W) Input.GetKey (KeyCode.LeftShift)) {
transform.Translate (Vector3.forward * shiftBonus, Space.World);
}
}
}
}