如何修复布尔值?
How to fix boolean value?
我正在尝试获取此 jump/movement 系统,地面检查的 bool 没有改变。当我尝试代码时,bool 的值是它的起始值。这是我的代码。代码是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);
}
}
如果你的游戏是2D的,你必须使用OnCollisionEnter/Exit
2D。
void OnCollisionEnter2D(Collision theCollision)
{
if (theCollision.gameObject.name == "floor")
{
isgrounded = true;
transform.Translate(0, 0, 1);
}
// ...
}
我正在尝试获取此 jump/movement 系统,地面检查的 bool 没有改变。当我尝试代码时,bool 的值是它的起始值。这是我的代码。代码是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);
}
}
如果你的游戏是2D的,你必须使用OnCollisionEnter/Exit
2D。
void OnCollisionEnter2D(Collision theCollision)
{
if (theCollision.gameObject.name == "floor")
{
isgrounded = true;
transform.Translate(0, 0, 1);
}
// ...
}