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

Question I dont know why my player movement script isnt working

Discussion in 'Scripting' started by mowmizyd, Oct 11, 2020.

  1. mowmizyd

    mowmizyd

    Joined:
    Oct 11, 2020
    Posts:
    8
    I also do everything from youtube tutorial
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6. {
    7.     public float walkSpeed;
    8.     public float sprintSpeed;
    9.     public float jumpHeight;
    10.  
    11.     [SerializeField] private float moveSpeed;
    12.  
    13.     private Rigidbody rb;
    14.  
    15.     // Use this for initialization
    16.     void Start ()
    17.     {
    18.         rb = GetComponent<Rigidbody>();
    19.  
    20.         moveSpeed = walkSpeed;
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update ()
    25.     {
    26.         PlayerMove();
    27.     }
    28.  
    29.     private void PlayerMove()
    30.     {
    31.         float MoveX = Input.GetAxis("Horizontal") * (moveSpeed * 0.8f) * Time.deltaTime;
    32.         float MoveX = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
    33.  
    34.         if (Input.GetButton("Sprint"))
    35.         {
    36.             moveSpeed = sprintSpeed;
    37.         }
    38.         else
    39.         {
    40.             moveSpeed = walkSpeed;
    41.         }
    42.  
    43.  
    44.         if(Input.GetButtonDown("Jump"))
    45.         {
    46.             if(rb.velocity.y <= 0.1f && rb.velocity.y >= -0.1f)
    47.             {
    48.                 rb.AddForce(new Vector3(rb.velocity.x, (rb.velocity.y + jumpHeight) * 10, rb.velocity.z));
    49.             }
    50.         }
    51.  
    52.         transform.Translate(new Vector3(MoveX, 0, MoveZ));
    53.    
    54.     }
    55.  
    56. }
    57.  
    View attachment 715114
    unity byczq.png
     
    Last edited: Oct 11, 2020
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    as you can see you have there 2 " { " , you need to remove one,

    also one at the bottom
     
  3. mowmizyd

    mowmizyd

    Joined:
    Oct 11, 2020
    Posts:
    8
    what lines
     
  4. mowmizyd

    mowmizyd

    Joined:
    Oct 11, 2020
    Posts:
    8
    aight got it next error is
     

    Attached Files:

    • mmm.png
      mmm.png
      File size:
      3.9 KB
      Views:
      258
  5. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    ye well, you can ready the line where it happens :)

    the error says exactly what it is , you declared 2x the same variable

    1. float MoveX = Input.GetAxis("Horizontal") * (moveSpeed * 0.8f) * Time.deltaTime;
    2. float MoveX = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
    Code (CSharp):
    1.         float MoveX = Input.GetAxis("Horizontal") * (moveSpeed * 0.8f) * Time.deltaTime;
    2.         float MoveX = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
     
  6. mowmizyd

    mowmizyd

    Joined:
    Oct 11, 2020
    Posts:
    8
    i dont get it what i need 2 do?
     
  7. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    you need to declare for ea. value a different variable :)

    Code (CSharp):
    1.         float MoveX = Input.GetAxis("Horizontal") * (moveSpeed * 0.8f) * Time.deltaTime;
    2.         float MoveY = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;