Animation controller script – Unity Asset Hub

using UnityEngine;

public class AnimationController : MonoBehaviour
{
    // The animator component of the game object
    private Animator animator;

    // The horizontal movement input axis name
    public string horizontalAxisName = "Horizontal";

    // The vertical movement input axis name
    public string verticalAxisName = "Vertical";

    // The parameter name for the horizontal movement float in the animator controller
    public string horizontalParamName = "Horizontal";

    // The parameter name for the vertical movement float in the animator controller
    public string verticalParamName = "Vertical";

    // The parameter name for the speed float in the animator controller
    public string speedParamName = "Speed";

    // Start is called before the first frame update
    void Start()
    {
        // Get the animator component
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        // Get the horizontal and vertical input values
        float horizontalInput = Input.GetAxisRaw(horizontalAxisName);
        float verticalInput = Input.GetAxisRaw(verticalAxisName);

        // Set the animator parameters based on the input values
        animator.SetFloat(horizontalParamName, horizontalInput);
        animator.SetFloat(verticalParamName, verticalInput);

        // Calculate the speed based on the input values
        float speed = new Vector2(horizontalInput, verticalInput).magnitude;

        // Set the animator speed parameter based on the speed value
        animator.SetFloat(speedParamName, speed);
    }
}

In this script, we first define a animator variable to store the Animator component of the game object.

We then define variables for the input axis names and animator parameter names. These variables can be set in the inspector for each game object that uses this script.

In the Start() function, we get the Animator component of the game object and store it in the animator variable.

In the Update() function, we get the horizontal and vertical input values using the Input.GetAxisRaw() function. We then set the animator parameters for the horizontal and vertical input using the animator.SetFloat() function.

We also calculate the speed based on the input values using the magnitude function of a Vector2 and set the animator speed parameter using the animator.SetFloat() function.

You can use this script on any game object that has an Animator component, and adjust the input and parameter names in the inspector to match your game’s input and animation needs.

To know more:Unity asset hub

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock