::キー入力(上下左右)で動く
>>改善すべき:もっと自然な数値(数式)設定?
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);
}
}
'Unity' 카테고리의 다른 글
Portfolio:事前調律 #1 (0) | 2020.11.19 |
---|---|
Portfolio:事前検証 #1 (0) | 2020.11.17 |
アカデミー#3:武器を変える。 (0) | 2020.10.29 |
アカデミー#2:目標たちを辿って宝箱を開ける。 (0) | 2020.10.29 |
アカデミー#1:目標まで移動してヒット (0) | 2020.10.28 |