RTS Camera Rotation Problems! New to Scripting

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);
				
						}
		}

	
	}
}

Why don’t you use Space.Self and keep the height of the camera manually. You can either do so by storing the height-position before your movementcode and then applying it again after the movement has taken place or you go for a new Vector3, do your speedcalculations (Vector3.right * threshold or whatever) and eliminate the heightchange there before you apply it to the translate function.

Just take out Space.world when you’re rotating the camera. This will rotate on its local axis rather than on the world y axis, so when you move it using space.world it will move forward regardless of its local rotation.

Thanks for the replies, yeah i know im newb. These didnt solve my issue maybe i can explain it easier. When i rotate the camera as it stands, when i move forward, the camera appears to be moving backwards because i rotated it say 180 degrees because i have it set to Move Forward based of world. If i use Space.Self then because my camera is initially rotated at a 45 degree angle to give it more of a 3d look as opposed to a straight top down, moving by self will move it through my floor.

So i need the camera to move based of World so it doesnt move through my floor when i tell it to move forward because im looking towards the ground, and when i rotate it i dont want it to make my controls get flipped around… I’ll keep digging of course but was hoping to figure this problem out a little quicker as i already spent hours on this ahah. No worries though guys if you can help then great, if not then most likely im not explaining it clear enough,

Thanks!

I got what you were saying. My solution should work. Maybe a code example.

        if (Input.GetKey (KeyCode.Q)) {

                        transform.Rotate (0, 1, 0);

                }

 

        if (Input.GetKey (KeyCode.E)) {

            transform.Rotate (0, -1, 0);

        }

I know this change should work, since I have functioning code that works that way.

this should work

//project screen center onto a ground plane

Ray ray = camera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2));

Plane plane = new Plane(Vector3.up, 0);
float dist;

Vector3 p1 = transform.position;
Vector3 p2 = plane.Raycast(ray, out dist) ? ray.GetPoint(dist) : p1;

//make sure both points are on the same plane

p1.y = p2.y;

//get directions

Vector3 forward = (p2 - p1).normalized;
Vector3 right = Vector3.Cross(forward, -Vector3.up);

//adjust position when keys are pressed

if (Input.GetKey(KeyCode.A)) {

	transform.position -= right * cameraSpeed;
}

if (Input.GetKey(KeyCode.D)) {

	transform.position += right * cameraSpeed;
}

if (Input.GetKey(KeyCode.S)) {

	transform.position -= forward * cameraSpeed;
}

if (Input.GetKey(KeyCode.W)) {

	transform.position += forward * cameraSpeed;
}

Make your camera a child of an empty GameObject. Use the parent to do rotations instead. This way the rotation stays “flat” and doesn’t mess up the downward facing angle of the camera.

Dude man!!! It Works! Now comes the task of figuring out why it works. New to C# so there’s a lot going on there i don’t understand, but thanks a lot mr malee and thanks to everyone who gave up their time to help me out.