Teachers open the door but You must enter by yourself.
Unity の公式キャラクター Unity Chan のモデルを使ってAnimator Component の基礎をマスターしましょう。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class UC : MonoBehaviour
{
Animator animator;
void Awake(){
var inputActionAsset
= ScriptableObject.CreateInstance<InputActionAsset>();
var map = inputActionAsset.AddActionMap("Player");
var action = map.AddAction(
"Move",
InputActionType.Value
);
action.AddCompositeBinding("2DVector")
.With("Up", "<Keyboard>/upArrow")
.With("Down", "<Keyboard>/downArrow")
.With("Left", "<Keyboard>/leftArrow")
.With("Right", "<Keyboard>/rightArrow")
.With("Up", "<Keyboard>/w")
.With("Down", "<Keyboard>/s")
.With("Left", "<Keyboard>/a")
.With("Right", "<Keyboard>/d");
action.started += OnRun;
action.canceled += OnStop;
var action2 = map.AddAction(
"Jump",
InputActionType.Button,
"<Keyboard>/Space"
);
action2.started += OnJump;
action2.canceled += OnJumpOff;
inputActionAsset.Enable();
}
void Start(){
animator = GetComponent<Animator>();
}
void OnRun(InputAction.CallbackContext c){
animator.SetFloat("Speed", 1f);
var vector2d = c.ReadValue<Vector2>();
var dx = vector2d.x;
var dy = vector2d.y;
var angle=0f;
if(dx>0.1f){//4方向に走るようにコードを追加しましょう
angle=90f;
}
transform.rotation = Quaternion.Euler(0, angle, 0);
}
void OnStop(InputAction.CallbackContext c){
animator.SetFloat("Speed", 0f);
}
void OnJump(InputAction.CallbackContext c){
animator.SetBool("Jump", true);
}
void OnJumpOff(InputAction.CallbackContext c){
animator.SetBool("Jump", false);
}
}
This site is powered by