我正在嘗試獲得這個跳躍/移動系統,并且地面檢查的布林值沒有改變。當我嘗試代碼時,布林值是它的起始值。這是我的代碼。代碼是c#,我使用的引擎是unity。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MOVEMENT : MonoBehaviour
{
public float playerJumpHeight;
public GameObject player;
public float speed;
public float PlayerXPosition;
public bool isgrounded = false;
public GameObject data_to;
void fixedupdate()
{
PlayerXPosition = player.transform.position.x;
}
void OnCollisionEnter(Collision theCollision)
{
if (theCollision.gameObject.name == "floor")
{
isgrounded = true;
transform.Translate(0,0,1);
}
if (theCollision.gameObject.name == "Height_Check")
{
isgrounded = false;
transform.Translate(0,0,0);
}
}
//consider when the character is jumping .. it will exit collision.
void OnCollisionExit(Collision theCollision)
{
if (theCollision.gameObject.name == "floor")
{
isgrounded = false;
}
}
void Update()
{
Rigidbody2D rb = GetComponent<Rigidbody2D>();
if (Input.GetKeyDown(KeyCode.W) && isgrounded == true) {
rb.AddForce(transform.up * speed);
}
if (Input.GetKey(KeyCode.A))
rb.AddForce(Vector2.left);
if (Input.GetKey(KeyCode.D))
rb.AddForce(Vector2.right);
}
}
uj5u.com熱心網友回復:
如果您的游戲是 2D,則必須使用OnCollisionEnter/Exit
2D。
void OnCollisionEnter2D(Collision theCollision)
{
if (theCollision.gameObject.name == "floor")
{
isgrounded = true;
transform.Translate(0, 0, 1);
}
// ...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/483840.html
上一篇:如何在DOTween中實作異步?