0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

アセット(残り1日メガバンドルセール)を使用して3Dキャラクターを自然に階段を上らせる

Posted at

残すところ後1日ちょっとですが、Unity Assets がプロトタイプピング・メガバンドルセールをやっています。

このメガバンドルに3Dキャラクターを簡単に動かすアセット(Easy Character Movement)が入っています。
このアセットだけで凸凹道も、階段も上れるし、高さによってはちゃんと引っかかるように設定もできます。

trim4.gif

無料のジョイスティックアセットを合わせれば、モバイルでもタッチでキャラクターを操作できます。
メガバンドルのアセットと無料のJoystickアセットと合わせて、操作する方法をまとめました。

1. アセットのインポート

2. Joystick の準備

  1. Hierarchy 上の空白スペースを左クリックし、UI -> Canvas を選択して Canvas を作成します
1. Fixed Joystick を今作った Canvas にドラッグアンドドロップします 1. GameとHierarchyの画面がこんな感じになっていると思います。 1. 画面真ん中上部のプレイボタンを押して、Joystick操作できるか確認します ![trim.gif](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/506075/be0b7f70-2306-019e-0a60-81ad22da5d69.gif)

3. 動かすキャラクターの準備

  1. Easy Character Movement の Demo シーンを開いて、Hierarchy 上の ECM_EthanCharacter を Projcet の Asset にドラッグアンドロップして prefab 化します。
1. SampleScene に戻ります。 1. Project の空いたスペースを右クリック、3D Object -> Plane を選択し、地面を作ります。 1. 1 で prefab 化した ECM_EthanCharacter を Hierarchy にドラッグアンドドロップします。 1. ここまで来ると、Game 画面がこんな感じで Ethan が後ろを向いていて、でかいジョイスティックがある状態になっているはずです。

4. Joystick と Easy Charactor Movement を繋ぐプログラム

  1. Project の Assets を右クリックして Create -> C# Script を選択し、OriginalCharacterController.cs(Unity 上は拡張子が表示されません)を作成します。
1. OriginalCharacterController.cs は以下の通り。基本は BaseCharacterController を継承して、HandleInput を override したところに、Joystick の入力値をセットしてあげるだけです!
using UnityEngine;
using ECM.Controllers;

public class OriginalCharacterController : BaseCharacterController
{
    private Joystick joystick;

    void Start()
    {
        joystick = FindObjectOfType<Joystick>();
    }
    
    protected override void HandleInput()
    {
        moveDirection = new Vector3
        {
            x = joystick.Horizontal,
            z = joystick.Vertical
        };
    }
}

5. Ethan にプログラムを設定

  1. prefab 化した ECM_EthanCharacter についている EthanCharacterController を削除します。
1. Add Componet ボタンを押して、先ほど作った MyCharacterController を追加します。

6. 実行して Ethan を Joystick で動かす

  1. 三角ボタンを押して実行します
  2. Ethan が立ったままですが、Joystick に合わせて動くはずです。
    trim2.gif

7. これでは物足りない人はこちら

先ほど作ったプログラムをこちらに更新して実行します。

  • HandleInput にジャンプとしゃがむ動作を入れています。
  • Animate メソッドを追加して、移動時などにアニメーションするようにしています。
using UnityEngine;
using ECM.Controllers;

public class OriginalCharacterController : BaseCharacterController
{
    private Joystick joystick;

    void Start()
    {
        joystick = FindObjectOfType<Joystick>();
    }

    protected override void HandleInput()
    {
        moveDirection = new Vector3
        {
            x = joystick.Horizontal,
            z = joystick.Vertical
        };

        jump = Input.GetKey(KeyCode.J);
        crouch = Input.GetKey(KeyCode.C);
    }
    
    protected override void Animate()
    {
        if (animator == null)  return;

        var move = transform.InverseTransformDirection(moveDirection);

        var forwardAmount = move.z;

        animator.SetFloat("Forward", forwardAmount, 0.1f, Time.deltaTime);
        animator.SetFloat("Turn", Mathf.Atan2(move.x, move.z), 0.1f, Time.deltaTime);
        animator.SetBool("OnGround", movement.isGrounded);
        animator.SetBool("Crouch", isCrouching);

        if (!movement.isGrounded)
            animator.SetFloat("Jump", movement.velocity.y, 0.1f, Time.deltaTime);

        var runCycle = Mathf.Repeat(animator.GetCurrentAnimatorStateInfo(0).normalizedTime + 0.2f, 1.0f);
        var jumpLeg = (runCycle < 0.5f ? 1.0f : -1.0f) * forwardAmount;

        if (movement.isGrounded)
            animator.SetFloat("JumpLeg", jumpLeg);
    }
}

こんな感じで動きます。ちゃんと Joystick の位置に応じて歩いたり走ったりしていますね。
キーボードのJキーを押すことでジャンプもできます。
trim3.gif

0
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?