我试图让Unity上的涂鸦跳跃,不同之处在于当我登陆平台时,会增加10点。然而,这只适用于第一个平台。我从0开始,当我进入第一个平台时,我得到10分,但它停在那里。
我的代码出了什么问题,请问为什么其他平台会继续发生这种情况?
public class PlatformScript : MonoBehaviour {
public int currentScore;
public Text displayScore;
public float jumpForce = 10f;
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.relativeVelocity.y <= 0) {
Rigidbody2D rb = collision.collider.GetComponent<Rigidbody2D> ();
if (rb != null) {
Vector2 velocity = rb.velocity;
velocity.y = jumpForce;
rb.velocity = velocity;
}
Destroy (gameObject);
currentScore += 10;
Update ();
}
}
void Update()
{
displayScore.text = "Score: " + currentScore;
}
}
此外,这是我的关卡生成页面。平台随机生成:
public class LevelGenerator : MonoBehaviour {
public GameObject platformPrefab;
public int numberOfPlatforms = 200;
public float levelWidth = 3f;
public float minY = 1f;
public float maxY = 3.2f;
void Start () {
Vector3 spawnPosition = new Vector3 ();
for (int i = 0; i < numberOfPlatforms; i++)
{
spawnPosition.y += Random.Range (minY, maxY);
spawnPosition.x = Random.Range (-levelWidth, levelWidth);
Instantiate (platformPrefab, spawnPosition, Quaternion.identity);
}
}
}
我可能已经注意到,我是Unity的新手。
答案 0 :(得分:2)
然而,这只适用于第一个平台。我从0开始 当我进入第一个平台时,我得到10分,但它就会停在那里。
那是因为你正在使用PlatformScript
销毁它(你Destroy (gameObject);
脚本附加到的游戏对象)。
如果PlatformScript
脚本附加到平台,并且您的目标是销毁其他GameObject,则使用Destroy(collision.gameObject);
代替Destroy (gameObject);
。
最后,不要手动调用Update
功能。 Unity会自动调用此函数。此外,最好在修改displayScore
时更新currentScore
文本,而不是Update
函数中的每一帧。
如果这不起作用,请使用Debug.Log
验证是否正在调用OnCollisionEnter2D
。还可以使用它来验证if (collision.relativeVelocity.y <= 0)
是否正在评估true
。如果这两个中的任何一个是false
,则您的分数不会更新。请参阅下面的固定OnCollisionEnter2D
功能。用它来调试你的代码。
您的新代码:
void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log("Collision detected!");
if (collision.relativeVelocity.y <= 0)
{
Debug.Log("RelativeVelocity <=0");
Rigidbody2D rb = collision.collider.GetComponent<Rigidbody2D>();
if (rb != null)
{
Vector2 velocity = rb.velocity;
velocity.y = jumpForce;
rb.velocity = velocity;
}
Destroy(collision.gameObject);
currentScore += 10;
displayScore.text = "Score: " + currentScore;
}
}
答案 1 :(得分:0)
你正在摧毁持有分数的平台。将数据保存在全局可访问(平台之间共享)对象(例如LevelGenerator
)中。通过一点点改组,它看起来像这样(static
用于简单化);
public class PlatformScript : MonoBehaviour
{
public float jumpForce = 10f;
void OnCollisionEnter2D (Collision2D collision)
{
if (collision.relativeVelocity.y <= 0)
{
Rigidbody2D rb = collision.collider.GetComponent<Rigidbody2D>();
if (rb != null)
{
Vector2 velocity = rb.velocity;
velocity.y = jumpForce;
rb.velocity = velocity;
}
// update our global score
LevelGenerator.currentScore += 10;
Destroy(gameObject);
}
}
}
public class LevelGenerator : MonoBehaviour
{
// global persistent data
public static int currentScore;
public Text displayScore;
public GameObject platformPrefab;
public int numberOfPlatforms = 200;
public float levelWidth = 3f;
public float minY = 1f;
public float maxY = 3.2f;
void Start ()
{
Vector3 spawnPosition = new Vector3();
for (int i = 0; i < numberOfPlatforms; i++)
{
spawnPosition.y += Random.Range(minY, maxY);
spawnPosition.x = Random.Range(-levelWidth, levelWidth);
Instantiate(platformPrefab, spawnPosition, Quaternion.identity);
}
}
void Update ()
{
displayScore.text = "Score: " + currentScore;
}
}