LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

Unityでキャラクターを歩かせてみたの備忘録(自分用)

Last updated at Posted at 2023-12-25
1 / 2

オブジェクト設定

blender.png
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);
        }
    }
}

アニメータ設定

animator_2.png

  1. Assetの部分で右クリック
  2. create -> animator

animator_3.png

  1. 黒い格子状の部分で右クリック -> empty
  2. ②の上にある+ボタンをクリックでパラメータの作成
  3. ①で作ったemptyに対して右クリック -> make transition
  4. Conditionの設定

animator_4.png
歩くモーションは繰り返すので、その設定を行わなければならない。上の写真の順番でクリックすればできる。

まとめ

今回でunityでキャラを動かそうということをしたのでその備忘録を書かせていただきました。 アニメータの設定ではループ設定を忘れないようにですね。

あとがき

メリークリスマス。今年も早いことでもうクリスマスです。相変わらずクリぼっちです。
さて、アドベントカレンダーは最終日となりました。最終日は私の担当です。
12月は忙しくて大変でした。疲れた~~~~。でもまだ「今年も1年お疲れさまでした」とは言わないでおきます。明日はハッカソンですからね。
では明日も頑張りましょう!

0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up