- Home /
My camera is not linking with my movement
Im currently making a FPS anM i have already created my movement and look controls, but when i look in a certain direction and I press W, it moves me to another direction, I think my vector3 is linked to the world instead of my character, does anyone know how to solve this issue, this is my movement script:
private void Awake()
{
//This is to get the information of our rigidbody and store it in the rb variable.
rb = GetComponent<Rigidbody>();
}
private void Start()
{
}
private void Update()
{
MovementInputs();
Jumping();
}
private void FixedUpdate()
{
Movement();
}
private void MovementInputs()
{
//Those are for the inputs for the movement. This information is stored within the x and z variables.
x = Input.GetAxisRaw("Horizontal");
z = Input.GetAxisRaw("Vertical");
}
private void Movement()
{
//This is to create the movement and change our rigidbody's velocity based on the x and z which are multiplied by the walk speed.
Vector3 movement = rb.velocity = new Vector3(x * walkSpeed, rb.velocity.y, z * walkSpeed);
}
private void Jumping()
{
//This is to get the input which is space and if it is pressed.
if (Input.GetKeyDown(KeyCode.Space))
{
//If the space button is pressed, the velocity will change, the x and z will stay the same but the y will change based on the jump force applied.
rb.velocity = new Vector3(x, jumpForce, z);
}
}
} and this is my look controls script: private void Start() { playerBody = transform.parent.gameObject; //This is to lock and make the cursor invisible while playing the game. Cursor.lockState = CursorLockMode.Locked; Cursor.visible = false; } private void Update() { CameraRotation(); } private void FixedUpdate() {
}
private void CameraRotation()
{
//This is to get the values of the axis from the Mouse X and Mouse Y which are shown in unity.
Vector2 inputValues = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
//This is for the up, down, left and right movement and to scale down the movement of the camera by smoothing and the sensitivity.
inputValues = Vector2.Scale(inputValues, new Vector2(mouseSensitivity * mouseSmoothing, mouseSensitivity * mouseSmoothing));
//This is to let the camera movement exist and to create a smoothing effect so the camera doesnt suddenly stop.
smoothedVelocity.x = Mathf.Lerp(smoothedVelocity.x, inputValues.x, 1f / mouseSmoothing);
smoothedVelocity.y = Mathf.Lerp(smoothedVelocity.y, inputValues.y, 1f / mouseSmoothing);
currentlookingPos += smoothedVelocity;
//Everything written in this bracket above is made for the calculations. Everything down now is for the actual movement.
//wtf are quaterions?????
transform.localRotation = Quaternion.AngleAxis(-currentlookingPos.y, Vector3.right);
playerBody.transform.localRotation = Quaternion.AngleAxis(currentlookingPos.x, playerBody.transform.up);
//This is to stop the full camera rotation infinite thingy, idk.
currentlookingPos.y = Mathf.Clamp(currentlookingPos.y, -80f, 80f);
}
}
Thanks and take care everyone.
check if your player body is actually rotating when you look around
Your answer
Follow this Question
Related Questions
Automatic Camera Rotation in the 3D Game Kit 0 Answers
How to pan the camera depending on the zoom 1 Answer
Cinemachine input axis camera movement,cinemachine input axis control with script 1 Answer
How to allow camera to complete an upside down rotation while using LookAt() 0 Answers
Unity - Dancing Ball World camera following and rotating system 1 Answer