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

Bug Move script doesn't work

Discussion in 'Scripting' started by switchoff482, May 7, 2023.

  1. switchoff482

    switchoff482

    Joined:
    May 6, 2023
    Posts:
    4
    So I have a movement script that should allow my player capsule to move and jump. The movement works but my first-person camera starts spinning like crazy when I move. Jumping also doesn't seem to work at all.

    My PlayerMovement script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     public float moveSpeed = 5f;
    8.     public float jumpForce = 5f;
    9.     public float gravityScale = 2f;
    10.     public Transform groundCheck;
    11.     public LayerMask groundLayer;
    12.  
    13.     private CharacterController controller;
    14.     private Vector3 motion;
    15.     private bool isGrounded;
    16.  
    17.     void Start()
    18.     {
    19.         controller = GetComponent<CharacterController>();
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         float horizontalInput = Input.GetAxis("Horizontal");
    25.         float verticalInput = Input.GetAxis("Vertical");
    26.         isGrounded = Physics.CheckSphere(groundCheck.position, 0.2f, groundLayer);
    27.  
    28.         if (isGrounded && motion.y < 0)
    29.         {
    30.             motion.y = -2f;
    31.         }
    32.  
    33.         Vector3 moveDirection = transform.right * horizontalInput + transform.forward * verticalInput;
    34.         controller.Move(moveDirection * moveSpeed * Time.deltaTime);
    35.  
    36.         if (Input.GetButtonDown("Jump") && isGrounded)
    37.         {
    38.             motion.y = Mathf.Sqrt(jumpForce * -2f * Physics.gravity.y);
    39.         }
    40.  
    41.         motion.y += Physics.gravity.y * gravityScale * Time.deltaTime;
    42.         controller.Move(motion * Time.deltaTime);
    43.     }
    44. }
    45.  
    My FirstPersonCamera script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FirstPersonCamera : MonoBehaviour
    6. {
    7.     public Transform playerBody;
    8.     public float mouseSensitivity = 100f;
    9.  
    10.     float xRotation = 0f;
    11.  
    12.     void Start()
    13.     {
    14.         Cursor.lockState = CursorLockMode.Locked;
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    20.         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    21.  
    22.         xRotation -= mouseY;
    23.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    24.  
    25.         playerBody.Rotate(Vector3.up * mouseX);
    26.  
    27.         transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    28.     }
    29. }
    30.  

    I started today by the way, I use ChatGPT for coding. If you give me a code please explain to me what it does so I can learn how to code. :)
     
  2. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    I don't thing this is how it works. I think people here can help you with some issue but will not give you code like ChatGPT , all informations are or internet already. Also i am pretty sure it is not a "Bug".


    You can start here:

    How to make a Video Game - Getting Started - YouTube
    Code Monkey - YouTube
    Brackeys - YouTube

    This one is very internesting but not rly for beginner imo.
    Coding Adventure: Boids - YouTube
     
    Last edited: May 7, 2023
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    Sigh.

    You're going to have to learn properly. Chat GPT is only really useful if you have some idea of what you're going already.

    Start here: https://learn.unity.com/pathway/junior-programmer
     
    mopthrow and Chubzdoomer like this.
  4. switchoff482

    switchoff482

    Joined:
    May 6, 2023
    Posts:
    4
    I watched some Brackeys tutorials and managed to fix everything, thanks a lot @MartinMa_ :)
     
    MartinMa_ likes this.
  5. switchoff482

    switchoff482

    Joined:
    May 6, 2023
    Posts:
    4
    chat gpt explains every part of the code which is why I use it, its pretty good imo
     
  6. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    Yes imo Chat GPT > Stack overflow but you still need to know what to ask Chat gpt :)