::キー入力(上下左右)で動く

youtu.be/5nzEYsQzBNw

>>改善すべき:もっと自然な数値(数式)設定?

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using UnityEngine;

public class HeroA : MonoBehaviour
{
    // :: Variables : Easy to Use
    private CharacterController controller;
    private Animation anim;
    // :: Enums : Animation Key
    private enum eAnimation
    {
        [Description("idle@loop")]
        IDLE,
        [Description("run@loop")]
        RUN,
        [Description("walk@loop")]
        WALK
    }


    // Start is called before the first frame update
    void Start()
    {
        // :: Easy to use for : CharacterController
        controller = this.gameObject.GetComponent<CharacterController>();
        // :: Easy to use for : Animation
        anim = this.gameObject.GetComponent<Animation>();
    }

    // :: for use enum Description
    private string GetEnumDescription(eAnimation value)
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());
        var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

        var descriptionString = attributes.Select(ele => ele.Description).FirstOrDefault();

        if(descriptionString != null)
        {
            return descriptionString;
        }
        return value.ToString();
    }

    // Update is called once per frame
    void Update()
    {
        // :: 1.1 Input key (up, down) check
        float forwardInput = Input.GetAxisRaw("Vertical");

        // :: 2 Rotate with key (left, right) check
        float rotateInput = Input.GetAxisRaw("Horizontal");
        this.transform.Rotate(Vector3.up * rotateInput * 0.5f);

        // :: 3# Change Animation
        // ::: Run
        if (forwardInput != 0)
        {
            anim.Play(this.GetEnumDescription(eAnimation.RUN));

            // :: 1.2 Move forward and Backward
            this.controller.Move(this.transform.forward * forwardInput * Time.deltaTime);
        }
        // ::: Just Rotate
        else if(rotateInput != 0){
            anim.Play(this.GetEnumDescription(eAnimation.WALK));
        }
        // ::: No Move
        else
        {
            anim.Play(this.GetEnumDescription(eAnimation.IDLE));
        }


        //this.transform.Rotate(0, 1, 0);
        //controller.Move(this.transform.forward * 0.01f);
    }
}
블로그 이미지

RIsN

,