Character movement relative to the camera

Hi,

I got a Camera with a MouseOrbit-Script attached, and a Character with a absic controll script,
i need help with making my character move relative to the camera, so if he moves forward he walks away from the camera and if he walks back he moves towards it. Even if i turn the camera. So that i would walk a full circle around an objct that i am locked on just by going left or right.

In the end it should behave like in Skyrim that i can controll the mouse and the player seperately but in relation to each other.

So i prepared some code to choose which direction he should face determined on which key is pressed, and the unity basic move script:

if(Input.GetKey ("w") || Input.GetKey ("a") || Input.GetKey ("s") || Input.GetKey ("d"))
		{
			_characterState = CharacterState.Walking;
			if (Input.GetKey ("w")) 
			{
                           //go forward
			} else if (Input.GetKey ("s")) 
			{
                           //go backward
			} else if (Input.GetKey ("d")) 
			{
                           //go right
			}else if (Input.GetKey ("a")) 
			{
                           //go left
			}
		}

		if (_characterController.isGrounded) 
		{
			moveDirection = new Vector3(-Input.GetAxis("Horizontal"),0,-Input.GetAxis("Vertical"));
			moveDirection = transform.TransformDirection(moveDirection);

			if(_characterState == CharacterState.Walking)
			{
				moveDirection *= walkSpeed;
			}else if(_characterState == CharacterState.Running)
			{
				moveDirection *= runSpeed;
			}
			if(Input.GetButton("Jump"))
			{
				moveDirection.y = jumpSpeed;
			}
		}
		moveDirection.y -= gravity * Time.deltaTime;
		_characterController.Move (moveDirection * Time.deltaTime);

I tried to put the movement in a method so i could acces it if the pressed key was determined and send the information which direction it should go this way, but this somehow interfered with my animations and i don’t know why because it just checks which animation is active right now and thats it… or it was messing with my CharacterStates… somehow…

Thanks in advance :3

PS: Sry for my probably bad english ^^"

As requested here are some Screenshots, even if ther isn’t much to show:

Screenshot1

Screenshot2

THIS WORKS FINE WITH A CHARACTER-CONTROLLER AND A CAMERA WITH UNITY’S MOUSE-ORBIT SCRIPT TO MAKE A THIRD PERSON GAME WHERE I CONTROLL THE CAMERA WITH MY MOUSE AND THE CHARACTER SEPERATELY WITH KEYBOARD OR A GAMEPAD!!! (For Perople who are searching exactly that!)

Obligatory example video —> Click Me

So, i solved the problem on my own:

I made the character walk in the right direction and relative to the camera using another movement-script i found:

if (_characterController.isGrounded) 
		{
			Vector3 forward = _camera.transform.TransformDirection(Vector3.forward);
			forward.y = 0;
			forward = forward.normalized;
			Vector3 right  = new Vector3(forward.z, 0, -forward.x);
			float h = Input.GetAxis("Horizontal");
			float v =Input.GetAxis("Vertical");
			
			moveDirection  = (h * right  + v * forward);

			if(_characterState == CharacterState.Walking)
			{
				moveDirection *= walkSpeed;
			}else if(_characterState == CharacterState.Running)
			{
				moveDirection *= runSpeed;
			}
			
			if (Input.GetButton ("Jump")) 
			{
				moveDirection.y = jumpSpeed;
			}
		}

		moveDirection.y -= gravity * Time.deltaTime;
		_characterController.Move (moveDirection * Time.deltaTime);

But like that he would still always look in the same direction, so i made a new Method that turns him smoothly in the direction i want him to look, by sending a Vector3 containing that data and the method sets this in relation to the camera:

void getRotation (Vector3 toRotation)
	{
		Vector3 relativePos = _camera.transform.TransformDirection(toRotation);
		relativePos.y = 0.0f;
		Quaternion rotation = Quaternion.LookRotation(relativePos);
		transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * turnSpeed);
	}

I determine the pressed key pretty rudimentary but it works like i want it to so it’s ok for me, and he can walk in 8 axes:

    if (Input.GetKey ("w") && Input.GetKey ("d")) 
    			{
    				getRotation(new Vector3(-1f, 0f, -1f));
    			}else if (Input.GetKey ("w") && Input.GetKey ("a")) 
    			{
    				getRotation(new Vector3(1f, 0f, -1f));
    			}else if (Input.GetKey ("s") && Input.GetKey ("d")) 
    			{
    				getRotation(new Vector3(-1f, 0f, 1f));
    			}else if (Input.GetKey ("s") && Input.GetKey ("a")) 
    			{
    				getRotation(new Vector3(1f, 0f, 1f));
    			}else if (Input.GetKey ("w"))
    			{
    				getRotation(new Vector3(0f, 0f, -1f));
    			} else if (Input.GetKey ("s")) 
    			{
    				getRotation(new Vector3(0f, 0f, 1f));
    			} else if (Input.GetKey ("d")) 
    			{
    				getRotation(new Vector3(-1f, 0f, 0f));
    			}else if (Input.GetKey ("a")) 
    			{
    				getRotation(new Vector3(1f, 0f, 0f));
    			}