Newer
Older
HardPoint-Project-Abertay-University-Unity3D / Assets / Scripts / CharacterClasses / AnimationController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AnimationController : MonoBehaviour
{
    //List of Transforms of both classes models
    [SerializeField] private List<Transform> modelMercenaryTransforms;
    [SerializeField] private List<Transform> modelRogueTransforms;

    //Mouse Postion Transform Object
    [SerializeField] private Transform mousePosition;

    //Animators
    [SerializeField] private List<Animator> animatorsList;

    //Character flip angle
    [SerializeField] private float flipAngle;

    private HealthSystem healthSystem;
    private Rigidbody characterRB;

    private Vector3 direction = Vector3.right;

    //LocalScale of the models
    private Vector3 rogueLocalScale;
    private Vector3 mercenaryLocalScale;
    private Vector3 playerLocalScale;

    private float velocity_x;
    private float velocity_y;
    private float horizontalInput;

    private bool onGround;
    private bool jump;

    private IMovable playerMovement;

    // Start is called before the first frame update
    void Start()
    {

        rogueLocalScale = new Vector3(1f, 0.5f, 1f);
        mercenaryLocalScale = new Vector3(0.19f, 0.095f, 0.19f);
        playerLocalScale = new Vector3(1.4f, 3f, 1.4f);

        characterRB = GetComponent<Rigidbody>();
        healthSystem = GetComponentInChildren<HealthSystem>();
        jump = false;
        playerMovement = GetComponent<IMovable>();
    }

    // Update is called once per frame
    void Update()
    {
        onGround = playerMovement.OnGround();
        horizontalInput = Input.GetAxis("Horizontal");

        velocity_x = characterRB.velocity.x;
        velocity_y = characterRB.velocity.y;

        ActiveAnimatorObject();
        CharacterDirection();
    }

    //Character Death
    void Die(Animator animator)
    {
        if (healthSystem.GetHealthPercentage() <= 0)
        {
            animator.SetTrigger("Dead");
        }
    }

    //Character Jump Trigger Animation
    void Jump(Animator animator)
    {
        if (Input.GetButtonDown("Jump") && !jump)
        {
            animator.SetTrigger("Jump");
            jump = true;
        }

        if (onGround)
        {
            jump = false;
        }
    }

    //This function will check wich object is active and will set the animator parameters values and run animation functions
    void ActiveAnimatorObject()
    {
        foreach (Animator animator in animatorsList)
        {
            if (animator.gameObject.activeSelf == gameObject.activeSelf)
            {
                animator.SetBool("OnGround", onGround);
                animator.SetFloat("Speed", Mathf.Abs(horizontalInput));
                animator.SetFloat("Yvelocity", velocity_y);
                Jump(animator);
                Die(animator);
            }
        }
    }

    //This function will change the model direction according to the mouse or input
    void CharacterDirection() 
    {
        Vector3 directionToMouse = mousePosition.transform.position - transform.position;
        float angle = Vector3.SignedAngle(Vector3.right, directionToMouse.normalized, Vector3.forward);

        if (horizontalInput < 0 || angle > flipAngle || angle < -flipAngle)
        {
            direction = Vector3.left;
        }

        else if (horizontalInput > 0 || angle < flipAngle || angle > -flipAngle)
        {
            direction = Vector3.right;
        }

        foreach (Transform modelMercenaryTransform in modelMercenaryTransforms)
        {
            modelMercenaryTransform.localScale = new Vector3(direction.x * mercenaryLocalScale.x, mercenaryLocalScale.y, mercenaryLocalScale.z);
        }

        foreach (Transform modelRogueTransform in modelRogueTransforms)
        {
            modelRogueTransform.localScale = new Vector3(direction.x * rogueLocalScale.x, rogueLocalScale.y, rogueLocalScale.z);
        }

        transform.localScale = new Vector3(direction.x * playerLocalScale.x, playerLocalScale.y, playerLocalScale.z);
    }
}