Perform this.transform.forward movement relative to updated rotation

Hello,

First post on the forum, not new to C# but still relatively new to Unity 3D. I’m working on a game where player movement is camera movement for a RTS. So far so good, WASD does what is expected, can zoom with Scroll Wheel, Q/E pans, and Z/X tilts. I understand Vector3s and Quaternions to an adept degree mathematically, but in terms of the engine code, what do I need to do to have WASD move relative to the new angles presented by panning?

What happens in-game is that after panning left/right via Q/E, WASD movement seems to be referencing the initial plane. You can replicate by doing the following:

  1. Make a new project

  2. Make an empty GameObject named “World”, make a 3D Object->Plane as a child of “World” called “Ground” with a scale of (10, 1, 10)

  3. Make a new Asset->C# Script, paste my code, and drag the script to the “Main Camera” GameObject

  4. Set the serialized input values to the ones commented out in my code where they are declared

**Note - ** I understand that the use of the default “Main Camera” isn’t standard practice. This project so far is to see what needs to be done prior to generalizing my objects and code in a standard fashion. If curious about the intention, once generalized, this CameraMovement solution will be how multiple players move and look via FishNet player objects.

Below is my code with the serialized input values for my Header variables. Thank you so very much in advance!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraMovement : MonoBehaviour
{
	private Vector3 CameraPosition;
	private Quaternion CameraRotation;
	private Quaternion OriginalRotation;
	
	[Header("Camera Settings")]
	public float CameraSpeed; //1
	public float RotationSpeed; //1
	public float GUITopBorder; //50
	public float GUIBottomBorder; //50
	public float GUILeftBorder; //50
	public float GUIRightBorder; //50
	public Vector3 CameraPositionLimit; //(50, 10, 50)
	
    void Start()
    {
        CameraPosition = this.transform.position;
		CameraRotation = this.transform.rotation;
		OriginalRotation = CameraRotation;
    }

    void Update()
    {
		
		//WASD - Forward, Left, Backward, Right
		if (Input.GetKey(KeyCode.W) || Input.mousePosition.y >= Screen.height - GUITopBorder && !Input.GetKey(KeyCode.S)){
		CameraPosition += Vector3.forward * CameraSpeed / 50;
	}
	if (Input.GetKey(KeyCode.A) || Input.mousePosition.x <= GUILeftBorder && !Input.GetKey(KeyCode.D)){
		CameraPosition += Vector3.left * CameraSpeed / 50;
	}
	if (Input.GetKey(KeyCode.S) || Input.mousePosition.y <= GUIBottomBorder && !Input.GetKey(KeyCode.W)){
		CameraPosition += Vector3.back * CameraSpeed / 50;
	}
	if (Input.GetKey(KeyCode.D) || Input.mousePosition.x >= Screen.width - GUIRightBorder && !Input.GetKey(KeyCode.A)){
		CameraPosition += Vector3.right * CameraSpeed / 50;
	}
		
		//Scroll Wheel - Zoom In, Zoom Out
		if (Input.GetAxis("Mouse ScrollWheel") > 0f ){
			CameraPosition.y -= CameraSpeed;
		}
		if (Input.GetAxis("Mouse ScrollWheel") < 0f ){
			CameraPosition.y += CameraSpeed;
		}
		
		//QE Pan - Left, Right
		if (Input.GetKey(KeyCode.Q)){
			CameraRotation.y -= RotationSpeed / 250;
		}
		if (Input.GetKey(KeyCode.E)){
			CameraRotation.y += RotationSpeed / 250;
		}
		
		//ZX Tilt - Down, Up
		if (Input.GetKey(KeyCode.Z)){
			CameraRotation.x += RotationSpeed / 500;
		}
		if (Input.GetKey(KeyCode.X)){
			CameraRotation.x -= RotationSpeed / 500;
		}
		
		//Reset CameraRotation
		if (Input.GetKey(KeyCode.R)){
			CameraRotation = OriginalRotation;
		}
		
		//Clamps CameraPosition to game area
		CameraPosition.x = Mathf.Clamp(CameraPosition.x, -CameraPositionLimit.x, CameraPositionLimit.x);
		CameraPosition.y = Mathf.Clamp(CameraPosition.y, 2, CameraPositionLimit.y);
		CameraPosition.z = Mathf.Clamp(CameraPosition.z, -CameraPositionLimit.z, CameraPositionLimit.z);
		
		//Clamps CameraRotation to suitable angles
		CameraRotation.x = Mathf.Clamp(CameraRotation.x, 0f, 1f);
		CameraRotation.y = Mathf.Clamp(CameraRotation.y, -1f, 1f);
		
		//Updates Main Camera position and rotation
		this.transform.position = CameraPosition;
		this.transform.rotation = CameraRotation;
    }
	
}

Hello and welcome to the forum! Try using transform.forward and transform.right instead of using Vector3.forward, Vector3.left, Vector3.back, and Vector3.right. These two vectors are relative to the object’s local coordinate system and will update according to the object’s rotation.

Something like this:

//WASD - Forward, Left, Backward, Right
if (Input.GetKey(KeyCode.W) || Input.mousePosition.y >= Screen.height - GUITopBorder && !Input.GetKey(KeyCode.S)){
    CameraPosition += transform.forward * CameraSpeed / 50;
}
if (Input.GetKey(KeyCode.A) || Input.mousePosition.x <= GUILeftBorder && !Input.GetKey(KeyCode.D)){
    CameraPosition -= transform.right * CameraSpeed / 50;
}
if (Input.GetKey(KeyCode.S) || Input.mousePosition.y <= GUIBottomBorder && !Input.GetKey(KeyCode.W)){
    CameraPosition -= transform.forward * CameraSpeed / 50;
}
if (Input.GetKey(KeyCode.D) || Input.mousePosition.x >= Screen.width - GUIRightBorder && !Input.GetKey(KeyCode.A)){
    CameraPosition += transform.right * CameraSpeed / 50;
}

Let me know if it helps.