0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Unityでパリィシステムを作る!! 2日目

Posted at

お久しぶりです。
いきなりさぽってしまいましたが、とにかく2日目です。

2日目の成果

・Capsule ObjectだったプレイヤーのモデルをUnityちゃんに変更
・移動している方向へプレイヤーが向くように調整
・AnimatorControllerを作成し、スティック入力に応じてアニメーションが変わるように変更

こんなものでしょうか。

つまずいたところ

Animationを探すのが正直一番てこずりました...
まだ肝心のガードモーションを見つけられていないのでそこを探すのが急務になりそうです
(とはいえ一番作りたい部分なので最後の方に作る予定ですが)

ゲームの状況

いったんゲーム画面はこんな感じですね

プレイヤーの動く速度とアニメーション速度があっていない部分が気になりますし、足も不自然に交差していますがいったんアニメーションが遷移するようになったのでよしとします...

プレイヤーを動かしているScriptはいったん下のような感じです。

Player.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using System;

public class PlayerMove : MonoBehaviour
{
    public float moveSpeed = 5f;
    public GameObject guardObject;
    private Gamepad gamepad;
    private Vector3 latestPos; //前回のposition
    private Animator animator;

    // Start is called before the first frame update
    void Start()
    {
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        //接続しているGamePadを取得
        gamepad = Gamepad.current;
        if (gamepad == null)
        {
            return;
        }

        Vector3 diff = transform.position - latestPos; //前回からどこに進んだかをベクトルで取得
        latestPos = transform.position; //前回のpositionの更新

        //ベクトルの大きさが0.01以上の時に向きを変更する
        if (diff.magnitude > 0.01f) transform.rotation = Quaternion.LookRotation(diff);

        //左スティックの入力値を取得
        Vector2 stickinput = gamepad.leftStick.ReadValue();
        //左ショルダーボタンの入力値をbool型に変更して取得
        bool leftShoulderInput = Convert.ToBoolean(gamepad.leftShoulder.ReadValue());

        Vector3 movement = new Vector3(stickinput.x, 0, stickinput.y);
        transform.Translate(movement * moveSpeed * Time.deltaTime, Space.World);

        //スティック入力された値の絶対値を取得
        float stickInputX = MathF.Abs(stickinput.x);
        float stickInputY = MathF.Abs(stickinput.y);

        //取得した絶対値が0より大きい場合Walkアニメーションを再生
        if(stickInputX > 0 || stickInputY > 0)
        {
            animator.SetBool("Walk", true);
        }
        else
        {
            animator.SetBool("Walk", false);
        }
        
        //L1ボタンが押されたら発火
        if (leftShoulderInput)
        {
            Guard();
        }
    }

    //ガード用のコライダーをActiveに変更
    void Guard()
    {
        guardObject.SetActive(true);
    }
}

Update関数の中が膨らんできましたね...
次回はそこの整理もちょっと考えないとです。

次の課題

カメラが張り付いたままなのでプレイヤーに追従するようにしたいですね。
カメライメージとしてはGhost of tsushimaのようなカメラを作成しようと思っています。
単純にプレイヤーの子オブジェクトにするだけではうまくいかないのでそれようのScriptが必要かなと考えてます。
(Playerと同じく左スティックの入力を取りながら、右スティックの情報も取得するようなものが欲しいですね)
カメラが(時間内に)終われば、攻撃システム及び敵のシステム作成に取り掛かろうかなと思っています。

参考文献

https://futabazemi.net/unity/absolute-value
https://tomokun-games.com/unity-animator-setfloat/
https://www.hanachiru-blog.com/entry/2019/02/20/183552
https://zenn.dev/inolalala/scraps/2f8bb58d3bf4e3

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?