If I understand this correctly, you walk with WASD and this will be mapped to h and v variables, right?
And you want to rotate the Camera with Q and E. This does not affect the player.
The problem may be:
If you rotate the camera to the right, your looking vector will change to the left. If you now move your player by pressing W (forward) he will move forward. But in world space coordinates. To the player it will seem that he is moving to the right. So if you want the player to walk in the direction the camera is looking (and therefore the player) you will have to get the rotation from the camera to the player.
It would actually be best, if the player only controls the player and not the camera. The player should identify with the player and not the camera (if that is type of game you want)
For that, rotate the player as you are in this script and attach the camera to the player, but on top of him. If you want a smoooth rotation, take the rotation (only z axis) from the player and lerp the cameras rotation to this value.
I’ve attached an example package for you.
public class CamScript : MonoBehaviour
{
public Transform Target;
public float SmoothSpeed = 2f;
private void Update()
{
transform.rotation = Quaternion.Lerp(transform.rotation, Target.rotation, Time.deltaTime * SmoothSpeed);
transform.position = Vector3.Lerp(transform.position, Target.position, Time.deltaTime * SmoothSpeed);
}
}
public class Player : MonoBehaviour
{
public Rigidbody Rb;
public float WalkSpeed = 1f;
public float RotateSpeed = 2f;
void Update()
{
float forward = Input.GetKey(KeyCode.W) ? 1 : Input.GetKey(KeyCode.S) ? -1f : 0f;
float sideward = Input.GetKey(KeyCode.A) ? -1 : Input.GetKey(KeyCode.D) ? 1f : 0f;
Rotate();
Move(forward, sideward);
}
void Move(float forward, float sidewards)
{
Vector3 movement = transform.forward * forward + transform.right * sidewards;
movement = movement.normalized * WalkSpeed * Time.deltaTime;
movement.y = 0f;
Rb.MovePosition(transform.position + movement);
}
void Rotate()
{
if (Input.GetKey(KeyCode.E))
{
transform.Rotate(0, RotateSpeed * Time.deltaTime, 0, Space.World);
}
if (Input.GetKey(KeyCode.Q))
{
transform.Rotate(0, -RotateSpeed * Time.deltaTime, 0, Space.World);
}
}
}
4380826–397642–PlayerMovement.unitypackage (6.43 KB)