Teachers open the door but You must enter by yourself.

Open Media Lab.
オープンメディアラボ

Animator コンポーネント
Animator Component

Unity の公式キャラクター Unity Chan のモデルを使ってAnimator Component の基礎をマスターしましょう。

  1. Unity Chan のモデルを 公式サイト からダウンロード
  2. Unity Chan がシェーディングされない(ピンク色になる)時は UnityToon Shader をインストール
  3. Assets/UnityChan/Models/unitychan.fbx を Hierarchy ウインドウにドラッグ&ドロップしてインスタンス化
  4. インスタンス化した Unity Chan のモデルの Animator コンポーネントの Controller に、Assets/UnityChan/UnityChanLocomotions.controller をドラッグ&ドロップ
  5. unitychan に以下のコード UC.cs もアタッチ(InputSystemを要インストール)

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 Powered by MathJax