Search Unity

Why this code isn't working?

Discussion in 'Scripting' started by AndreicuA, Jun 25, 2019.

  1. AndreicuA

    AndreicuA

    Joined:
    Jun 25, 2019
    Posts:
    2
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class miscare : MonoBehaviour
    4. {
    5.     [SerializeField] private string horizontalInputName;
    6.     [SerializeField] private string verticalInputName;
    7.     [SerializeField] private float movementSpeed;
    8.  
    9.     private CharacterController charController;
    10.  
    11.     private void Awake()
    12.     {
    13.         charController = GetComponent<CharacterController>();
    14.     }  
    15.  
    16.     private void Update()
    17.     {
    18.         PlayerMovement();
    19.     }
    20.  
    21.     private void PlayerMovement()
    22.     {
    23.         float vertInput = Input.GetAxis(verticalInputName) * movementSpeed;
    24.         float horizInput = Input.GetAxis(horizontalInputName) * movementSpeed;
    25.  
    26.         Vector3 forwardMovement = transform.forward * vertInput;
    27.         Vector3 lateralMovement = transform.right * horizInput;
    28.  
    29.         charController.SimpleMove(forwardMovement + lateralMovement);
    30.  
    31.     }
    32. }
     
    Last edited: Jun 25, 2019
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,618
  3. AndreicuA

    AndreicuA

    Joined:
    Jun 25, 2019
    Posts:
    2
    no i didn't get an error mesage
     
  4. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Then it works perfectly.

    If you think it behaves wrongly, what's the intended behaviour, and what's the actual behaviour?
     
    Vryken and DaemonicDreams like this.
  5. saifshk17

    saifshk17

    Joined:
    Dec 4, 2016
    Posts:
    488
    Have you attached the script to your character controller gameObject? Is there any change in the transform of character controller gameObject?