Problem with camera when moving

Hello when i press Z the player go forward, but when i press a,d,s the camera glitch, and when i press space bar nothing appen.

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

public class PlayerMovement : MonoBehaviour
{

public CharacterController controller;

public Vector3 playerVelocity;

public float playerSpeed = 12f;
public float playerSpeedGravity = 2.0f;
public float PlayerGravityValue = -9.81f;
public float playerJumpHeight = 1.0f;

public bool playerIsGrounded;

// Update is called once per frame
void Update()
{

// movement of the player.

float x = Input.GetAxis(“Horizontal”);
float z = Input.GetAxis(“Vertical”);

Vector3 move = transform.right * x + transform.forward * z;

controller.Move(move * playerSpeed * Time.deltaTime);

//gravity of the player

playerIsGrounded = controller.isGrounded;
if (playerIsGrounded && playerVelocity.y < 0)
{
playerVelocity.y = 0f;
}

Vector3 Move = new Vector3(Input.GetAxis(“Horizontal”), 0, Input.GetAxis(“Vertical”));
controller.Move(move * Time.deltaTime * playerSpeedGravity);

if (move != Vector3.zero)
{
gameObject.transform.forward = move;
}

// Changes the height position of the player…
if (Input.GetButtonDown(“Jump”) && playerIsGrounded)
{
playerVelocity.y += Mathf.Sqrt(playerJumpHeight * -3.0f * PlayerGravityValue);
}

playerVelocity.y += PlayerGravityValue * Time.deltaTime;
controller.Move(playerVelocity * Time.deltaTime);
}
}

This doesn’t look like a camera control script, and a “camera glitch” can mean all sorts of things. Also, use CODE tags when posting code or assume no one will look through it.

1 Like

yeah this script is for player movement and gravity but when i have the camera control script and the player movement script, this work, but when i add the gravity script, the a,d,s control make camera glitch and the jump movement didn’t work.

there is the camera control script :

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

public class MouseLook : MonoBehaviour
{
public float mouseSensivity = 100f;

public Transform playerBody;

float xRotation = 0f;

void Start()
{
Cursor.lockState = CursorLockMode.Locked;

}

void Update()
{
float mouseX = Input.GetAxis(“Mouse X”) * mouseSensivity * Time.deltaTime;
float mouseY = Input.GetAxis(“Mouse Y”) * mouseSensivity * Time.deltaTime;

xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90, 90f);

transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);

playerBody.Rotate(Vector3.up * mouseX);
}
}

The first thing I’d try is switching the camera script to using LateUpdate. But you still need to be specific about what this camera glitch really is.

i change the camera script from Update to LateUpdate but nothing change, there is a video of what apen.
In the begin i press W and after i try A S D and the camera “glitch”, and again the jump script didn’t work.

https://www.youtube.com/watch?v=5wHyQ3INTYI

i check if i delete the MouseLook script the problem is resolved but, no.
So the problem is from PlayerMovement script.
( the first script i have posted )

ok i found the problem, the problem is that line of script

        Vector3 Move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        controller.Move(move * Time.deltaTime * playerSpeedGravity);

        if (move != Vector3.zero)
        {
            gameObject.transform.forward = move;
        }

But i didn’t know why that make camera glitch, can you help me ?