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?

More than 3 years have passed since last update.

[Unity] プレイヤーを動かす(Rigidbobyを使用)

Last updated at Posted at 2021-02-06

[Unity] Rigidbodyを使用してプレイヤーを動かす

1, PlayerにRigidbodyをつける
EF343F94-DA1D-40E0-8A90-2D87C7236F75.jpeg

2, 重力を使用しない場合はGravityのチャックを外す
6D9D97AB-D07F-4081-B617-261006BBAFED_4_5005_c.jpeg

3, PlayerControllerを作り、下記コードを記述する
(下記でキーボード入力で動作する)

PlayerController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    float x;
    float z;
    public float moveSpeed = 2f;

    Rigidbody rb;

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

    // Update is called once per frame
    void Update()
    {
        x = Input.GetAxisRaw("Horizontal");
        z = Input.GetAxisRaw("Vertical");
    }

    void FixedUpdate()
    {
        rb.velocity = new Vector3(x, 0, z) * moveSpeed;
    }
}

一度ここで動作の確認をする。

アニメーションの作成

1 , AnimatorControllerを作成し、PlayerのAnimatorにアタッチする
(PlayerにAnimatorがない場合はAddComponentから作成してあげる)
7847C176-3136-4535-A180-7EBB2E8F72E9.jpeg

2 , Speedのパラメータを作成し、MakeTransitionを設定。各ConditionsにSpeedを設定する。
HasExitTimeのチェックを外す、TransitionDuratiorは0にする。
38BF3F7C-F104-4B51-8C04-46E0E2B6C45B.jpeg
C8624C6E-8DD9-4ADC-8A1F-A50074370E41_4_5005_c.jpeg

3, 下記コメント箇所のコードを追加する

PlayerController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    float x;
    float z;
    public float moveSpeed = 2;

    Rigidbody rb;
    //アニメーターを設定
    Animator animator;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        //アニメーターを取得
        animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        x = Input.GetAxisRaw("Horizontal");
        z = Input.GetAxisRaw("Vertical");
    }

    void FixedUpdate()
    {
        rb.velocity = new Vector3(x, 0, z) * moveSpeed;
        //パラメータを取得
        animator.SetFloat("Speed", rb.velocity.magnitude);
    }
}

4, アニメーションのループがうまくいかない場合はLoopTimeとLoopPoseのチェックを確認する
CF7499DF-2789-4E8C-B5CB-F024211FB71A.jpeg

Playerの方向転換

1, 下記コメント部分を追加する

PlayerController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    float x;
    float z;
    public float moveSpeed = 2;

    Rigidbody rb;
    Animator animator;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        animator = GetComponent<Animator>();
    }

    void Update()
    {
        x = Input.GetAxisRaw("Horizontal");
        z = Input.GetAxisRaw("Vertical");
    }

    void FixedUpdate()
    {
        // 方向を取得
        Vector3 direction = transform.position + new Vector3(x, 0, z) * moveSpeed;
        // directionの方向に向く
        transform.LookAt(direction);
        rb.velocity = new Vector3(x, 0, z) * moveSpeed;
        animator.SetFloat("Speed", rb.velocity.magnitude);
    }
}
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?