Search Unity

Feedback How To Combine These 2 Scripts Into 1 (If possible)

Discussion in 'Scripting' started by Banditmayonnaise, Jun 22, 2019.

  1. Banditmayonnaise

    Banditmayonnaise

    Joined:
    Apr 3, 2019
    Posts:
    10
    Hi
    I'm making a third person practice game. I'm trying to make a effective Character Controller movement with some smooth Animation. (That i have downloaded from Mixamo)
    I have made 2 Scripts for my project; first script is for the character controller and movement, the second is for the animation.

    Code (CSharp):
    1. public class PlayerMovement : MonoBehaviour
    2. {
    3.     private float InputX, InputZ, _speed;
    4.     private Camera _cam;
    5.     private CharacterController _controller;
    6.     private float gravity;
    7.  
    8.     private Vector3 desiredMoveDirection;
    9.     [SerializeField]
    10.     float _rotationSpeed = 0.3f;
    11.     [SerializeField]
    12.     float allowRotation = 0.1f;
    13.     [SerializeField]
    14.     float _movementSpeed = 1f;
    15.     [SerializeField]
    16.     float gravitymultiplier = 0.5f;
    17.  
    18.  
    19.     void Start()
    20.     {
    21.  
    22.         _controller = GetComponent<CharacterController>();
    23.         _cam = Camera.main;
    24.  
    25.  
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.         InputX = Input.GetAxis("Horizontal");
    32.         InputZ = Input.GetAxis("Vertical");
    33.  
    34.         InputDecider();
    35.         MovementManager();
    36.  
    37.     }
    38.  
    39.  
    40.  
    41.     void InputDecider()
    42.     {
    43.         _speed = new Vector2(InputX, InputZ).sqrMagnitude;
    44.  
    45.         if(_speed > allowRotation)
    46.         {
    47.             RotationManager();
    48.         }
    49.         else
    50.         {
    51.             desiredMoveDirection = Vector3.zero;
    52.         }
    53.     }
    54.  
    55.  
    56.     void RotationManager()
    57.     {
    58.         var forward = _cam.transform.forward;
    59.         var right = _cam.transform.right;
    60.  
    61.         forward.y = 0f;
    62.         right.y = 0f;
    63.  
    64.         forward.Normalize();
    65.         right.Normalize();
    66.  
    67.         desiredMoveDirection = forward * InputZ + right * InputX;
    68.  
    69.         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(desiredMoveDirection), _rotationSpeed);
    70.     }
    71.  
    72.     void MovementManager()
    73.     {
    74.         gravity -= 9.81f * Time.deltaTime;
    75.         gravity = gravity * gravitymultiplier;
    76.  
    77.         Vector3 moveDirection = desiredMoveDirection * (_movementSpeed * Time.deltaTime);
    78.         moveDirection = new Vector3(moveDirection.x, gravity, moveDirection.z);
    79.         _controller.Move(moveDirection);
    80.  
    81.         if(_controller.isGrounded)
    82.         {
    83.             gravity = 0f;
    84.         }
    85.     }
    86. }
    Animation Script:
    Code (CSharp):
    1. public class WalkingAnimation : MonoBehaviour
    2. {
    3.  
    4.     [SerializeField]
    5.     private Animator _animator;
    6.     [SerializeField]
    7.     private float inputX;
    8.     float inputZ;
    9.  
    10.  
    11.     void Start()
    12.     {
    13.         _animator = this.gameObject.GetComponent<Animator>();
    14.  
    15.  
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.  
    22.         inputX = Input.GetAxis("Horizontal");
    23.         _animator.SetFloat("InputX", inputX);
    24.  
    25.         inputZ = Input.GetAxis("Vertical");
    26.         _animator.SetFloat("InputZ", inputZ);
    27.     }
    28. }
    I'm really bad at scripting, so I dont know how to combine these 2 Scripts into 1.