Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Problem with camera when moving

Discussion in 'Getting Started' started by Flush286, Oct 30, 2020.

  1. Flush286

    Flush286

    Joined:
    Oct 15, 2020
    Posts:
    6
    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);
    }
    }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
    JoeStrout likes this.
  3. Flush286

    Flush286

    Joined:
    Oct 15, 2020
    Posts:
    6
    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.
     
  4. Flush286

    Flush286

    Joined:
    Oct 15, 2020
    Posts:
    6
    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);
    }
    }
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
  6. Flush286

    Flush286

    Joined:
    Oct 15, 2020
    Posts:
    6
    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.
     
  7. Flush286

    Flush286

    Joined:
    Oct 15, 2020
    Posts:
    6
    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 )
     
  8. Flush286

    Flush286

    Joined:
    Oct 15, 2020
    Posts:
    6
    ok i found the problem, the problem is that line of script

    Code (CSharp):
    1.         Vector3 Move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    2.         controller.Move(move * Time.deltaTime * playerSpeedGravity);
    3.  
    4.         if (move != Vector3.zero)
    5.         {
    6.             gameObject.transform.forward = move;
    7.         }
    But i didn't know why that make camera glitch, can you help me ?