オブジェクト設定
blenderからfbxファイルを作成し、使用している。アニメーションはstand、walkを作成している。
コード
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class controller : MonoBehaviour
{
public float speed = 0.01f;
Vector3 chara_move;
float magnitude;
Animator anime;
// Start is called before the first frame update
void Start()
{
anime = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.A))
{
transform.position += new Vector3(-1.0f * speed, 0f, 0f);
this.transform.rotation = Quaternion.Euler(0f, -90f, 0f);
anime.SetBool("walk", true);
}
else if (Input.GetKey(KeyCode.D))
{
transform.position += new Vector3(1.0f * speed, 0f, 0f);
this.transform.rotation = Quaternion.Euler(0f, 90f, 0f);
anime.SetBool("walk", true);
}
else if (Input.GetKey(KeyCode.W))
{
transform.position += new Vector3(0f, 0f, 1.0f * speed);
this.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
anime.SetBool("walk", true);
}
else if (Input.GetKey(KeyCode.S))
{
transform.position += new Vector3(0f, 0f, -1.0f * speed);
this.transform.rotation = Quaternion.Euler(0f, 180f, 0f);
anime.SetBool("walk", true);
}
else
{
anime.SetBool("walk", false);
}
}
}
アニメータ設定
- Assetの部分で右クリック
- create -> animator
- 黒い格子状の部分で右クリック -> empty
- ②の上にある+ボタンをクリックでパラメータの作成
- ①で作ったemptyに対して右クリック -> make transition
- Conditionの設定
歩くモーションは繰り返すので、その設定を行わなければならない。上の写真の順番でクリックすればできる。
まとめ
今回でunityでキャラを動かそうということをしたのでその備忘録を書かせていただきました。 アニメータの設定ではループ設定を忘れないようにですね。
あとがき
メリークリスマス。今年も早いことでもうクリスマスです。相変わらずクリぼっちです。
さて、アドベントカレンダーは最終日となりました。最終日は私の担当です。
12月は忙しくて大変でした。疲れた~~~~。でもまだ「今年も1年お疲れさまでした」とは言わないでおきます。明日はハッカソンですからね。
では明日も頑張りましょう!