実現したいこと
Unityにて3Dオブジェクトに移動とジャンプさせるスクリプトを適用したい。
前提
まったくの初心者です。
Unity3D URPで球体を動かすために、インターネット上にある移動するためのコードとジャンプするためのコードを組み合わせたのですが、ジャンプしてくれません。
それぞれ単独で走らせると正常に動作します。
すべてが初心者なので基本的なところが理解できていないのだと思いますが、しばらく試行錯誤しても解決しなかったため、質問させてください。
該当のソースコード
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
private Rigidbody m_rigid_body = null;
public float jumpPower;
private Rigidbody rb;
private bool isJumping = false;
void Start()
{
m_rigid_body = this.GetComponent<Rigidbody>();
rb = GetComponent<Rigidbody>();
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Space) && !isJumping)
{
rb.velocity = Vector3.up * jumpPower;
isJumping = true;
}
}
// ★★追加
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
// WASDで移動
void FixedUpdate()
{
Vector3 direction = Vector3.zero;
if (Input.GetKey(KeyCode.D)) { direction += new Vector3(1.0f, 0.0f, 0.0f); }
if (Input.GetKey(KeyCode.A)) { direction += new Vector3(-1.0f, 0.0f, 0.0f); }
if (Input.GetKey(KeyCode.W)) { direction += new Vector3(0.0f, 0.0f, 1.0f); }
if (Input.GetKey(KeyCode.S)) { direction += new Vector3(0.0f, 0.0f, -1.0f); }
MyAddForce(direction);
}
private void MyAddForce(Vector3 direction)
{
m_rigid_body.AddForce(direction);
m_rigid_body.velocity = direction * Time.deltaTime * 1000.0f;
}
}
試したこと
ここに問題に対して試したことを記載してください。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。