본문 바로가기

카테고리 없음

유니티 게임엔진으로 캐릭터가 이동시 애니메이션이 호출/When a character moves to the Unity game engine, the animation is called.

728x90

This script gets the Animator component attached to the character in the Start() method and then moves the character and plays the appropriate animation based on input in the Update() method. It also checks if the space key is pressed and plays the "jp" animation while adding a force to the character's rigidbody to jump. Make sure to adjust the force value to your liking.

using UnityEngine;

public class CharacterMovement : MonoBehaviour
{
    private Animator anim; // Animator component attached to the character

    private void Start()
    {
        anim = GetComponent<Animator>();
    }

    private void Update()
    {
        // Get input axis for horizontal and vertical movement
        float hAxis = Input.GetAxis("Horizontal");
        float vAxis = Input.GetAxis("Vertical");

        // Move character based on input
        transform.position += new Vector3(hAxis, 0, vAxis) * Time.deltaTime;

        // Set animation based on input
        if (hAxis != 0)
        {
            anim.Play("rl");
        }
        else if (vAxis != 0)
        {
            anim.Play("fb");
        }

        // Check if space key is pressed and jump
        if (Input.GetKeyDown(KeyCode.Space))
        {
            anim.Play("jp");
            // Add force to character's rigidbody to jump
            Rigidbody rb = GetComponent<Rigidbody>();
            if (rb != null)
            {
                rb.AddForce(Vector3.up * 500f);
            }
        }
    }
}

 

728x90