Search Unity

Character Controller problem

Discussion in 'Animation' started by JungPS, Nov 7, 2021.

  1. JungPS

    JungPS

    Joined:
    Nov 7, 2021
    Posts:
    1
    I have problem with character controller
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Player : MonoBehaviour
    5. {
    6.     CharacterController characterController;
    7.     public float speed = 6.0f;
    8.     private Vector3 moveDirection = Vector3.zero;
    9.     Rigidbody rigidbody;
    10.     Animator animator;
    11.     void Start()
    12.     {
    13.         characterController = GetComponent<CharacterController>();
    14.         animator = GetComponent<Animator>();
    15.     }
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         if (characterController.isGrounded)
    20.         {
    21.             animator.SetBool("Run",false);
    22.  
    23.             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
    24.             moveDirection *= speed;
    25.             if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0){
    26.                 animator.SetBool("Run",true);
    27.             }
    28.  
    29.             if(Input.GetAxis("Vertical") < 0){
    30.                 this.transform.rotation = Quaternion.Euler(0,180,0);
    31.             }else{
    32.                 this.transform.rotation = Quaternion.Euler(0,0,0);
    33.             }
    34.         }
    35.         characterController.Move(moveDirection * Time.deltaTime);
    36.     }
    37. }
    38.  
    Is there anything wrong?