Moving toward the camera direction

Hello, I’m pretty new to unity and game development, I’m doing turn based dungeon crawler in first person. Imagine something like legend of grimrock or might and magic legacy x. I’ve got a problem and I think I know how to solve it, but I don’t know if it is the best way to do it.

To make the first person camera move and rotate I’ve created this script:

private bool walking;
	private bool rotating;
	
	private Vector3 startPos;
	private Vector3 endPos;
	private float speed;
	private float startTime;
	private float journeyLength;
	private string direction;
	private int count;
	
	
	// Use this for initialization
	void Start () {
		walking = false;
		rotating = false;
		speed = 3.5f;
		startPos = transform.position;
		endPos = transform.position;
        journeyLength = Vector3.Distance(startPos, endPos);
		startTime = Time.time;
		count = 0;
	}
	
	void Update () {
		
		if (!walking  !rotating)
		{
			if(Input.GetAxis("Horizontal") > 0)
			{
				//Mover para a direita
				endPos = new Vector3(transform.position.x, transform.position.y, transform.position.z + 3f);
				walking = true;
			}
			else if(Input.GetAxis("Horizontal") < 0)
			{
				//Mover para a esquerda
				endPos = new Vector3(transform.position.x, transform.position.y, transform.position.z - 3f);
				walking = true;
			}
			else if (Input.GetAxis("Vertical") > 0)
			{
				//Mover para a frente
				endPos = new Vector3(transform.position.x - 3f, transform.position.y, transform.position.z);
				walking = true;
			}
			else if (Input.GetAxis("Vertical") < 0)
			{
				//Mover para tras
				endPos = new Vector3(transform.position.x + 3f, transform.position.y, transform.position.z);
				walking = true;
			}
			else if (Input.GetKey(KeyCode.Q))
			{
				//Rodar para a esquerda
				direction = "left";
				rotating = true;
			}
			else if (Input.GetKey(KeyCode.E))
			{
				//Rodar para a direita
				direction = "right";
				rotating = true;
			}
			else return;
			startPos = transform.position;
			journeyLength = Vector3.Distance(startPos, endPos);
			startTime = Time.time;
		}
		else if (walking)
		{
			Move();
		}
		else if (rotating)
		{
			Rotate();
		}
	}
	
	void Move()
	{
		float distCovered = (Time.time - startTime) * speed;
    	float fracJourney = distCovered / journeyLength;
    	transform.position = Vector3.Lerp(startPos, endPos, fracJourney);
		walking = (transform.position != endPos);
	}
	
	void Rotate()
	{
		if (direction == "right")
		{
			transform.Rotate(0, 2f, 0);
			count++;
		}
		else if (direction == "left")
		{
			transform.Rotate(0, -2f, 0);
			count++;
		}
		if (count == 45)
		{
			rotating = false;
			count = 0;
		}
	}

Now what I need is that instead of walking in for example the z axis when I press left I want it to move left according to the camera orientation, so if the camera is rotated to the right, he would need to move in the x axis. The solution I found is to make some “ifs” and make it manually, this isn’t really smart I think. Is there a easier way to do it?

You would convert the Camera x-Axis (if sideways is your goal ;)) into world space, and then simply add it to the current position of the player, to get the end position.

Example:

//Get the x-axis direction of the camera as a world vector. You need to either have a camera tagged as mainCamera, or make a reference to it in your script
Vector3 cameraRightWorld = Camera.main.transform.TransformDirection(Vector3.right);

if(Input.GetAxis("Horizontal") > 0 || Input.GetAxis("Horizontal") < 0)

{

                //Mover para a direita
//Take the current position of the player, and add the world vector. Multiply the GetAxis factor on to either reverse it, or keep it as is. Multply the vector scale on top (which you seem to have set as 3) A position plus a direction (with a length. In your case 3), will give you a new position ;)

         endPos = transform.position + cameraRightWorld*Input.GetAxis("Horizontal")*3;

         walking = true;

 }

This is untested code! Just to give you an idea… :slight_smile:

Thanks, but I don’t think you understood what is my problem. There’s no player, it’s first person and there’s only the camera instead of a character, like this: http://www.youtube.com/watch?v=RKvMDNsEc14

When the camera rotates in it’s axis I need to move where it’s facing. Your code almost worked, but something is weird, with my code, the 3 correction I was adding or removing made the game work like it should, you press a direction and it walks exactly one tile (wich is 3x3), like in Legend of Grimrock, but now it’s not working, it keeps walking if you hold the button.