在unity3d c#中创建突破游戏-砖块不会被摧毁
我用unity教程制作了一个突破游戏,一切似乎都很好,但是当我在游戏模式下玩的时候,出现了一些错误。因此,在我尝试修复错误后,砖块将不再被破坏。我试图撤销它,重新编写代码,甚至重新编写同一个项目,但没有任何效果。我能做什么 这些是我游戏的代码,与教程中的代码完全相同 桨:在unity3d c#中创建突破游戏-砖块不会被摧毁,c#,unity3d,destroy,C#,Unity3d,Destroy,我用unity教程制作了一个突破游戏,一切似乎都很好,但是当我在游戏模式下玩的时候,出现了一些错误。因此,在我尝试修复错误后,砖块将不再被破坏。我试图撤销它,重新编写代码,甚至重新编写同一个项目,但没有任何效果。我能做什么 这些是我游戏的代码,与教程中的代码完全相同 桨: public class Paddle : MonoBehaviour { public float paddleSpeed = 1f; private Vector3 playerPos = new Vector3(0
public class Paddle : MonoBehaviour {
public float paddleSpeed = 1f;
private Vector3 playerPos = new Vector3(0, -9f, 0);
void Update ()
{
float xPos = transform.position.x + (Input.GetAxis("Horizontal")*paddleSpeed);
playerPos = new Vector3(Mathf.Clamp(xPos, -7.5f, 7.5f), -9f, 0f);
transform.position = playerPos;
}
}
球:
死亡地带:
public class deathZone : MonoBehaviour {
void OnTriggerEnter(Collider col)
{
GM.instance.loseLife();
}
}
问题似乎出在对撞机上。您应该在编辑器中检查它们是否放置正确(因为不会写入debug.log on collision)。还要确保您使用的是碰撞器,而不是colliders2D(碰撞由Different方法调用)问题似乎出在碰撞器上。您应该在编辑器中检查它们是否放置正确(因为不会写入debug.log on collision)。另外,请确保您使用的是碰撞器,而不是colliders2D(碰撞由Different方法调用)在本教程中,您必须确保所有bricks游戏对象都具有bricks脚本和BoxCollider组件。此外,球应具有刚体和球体准直器组件
调试冲突或触发器的一种简单方法是简单地使用
debug.Log(“某物”)
作为OnCollisionCenter/OnTriggerCenter/中的第一个命令。。。方法。在本教程中,您必须确保所有bricks游戏对象都具有bricks脚本和BoxCollider组件。此外,球应具有刚体和球体准直器组件
调试冲突或触发器的一种简单方法是简单地使用
debug.Log(“某物”)
作为OnCollisionCenter/OnTriggerCenter/中的第一个命令。。。方法。是线销毁(游戏对象)代码>在类中调用?您能否尝试在本指令之前记录一些内容(Debug.log(“test”);
)?@Kardux感谢您的回复。我不熟悉unity,但根据我现在搜索的内容,控制台上应该会出现一个“测试”,对吗?但是什么也没发生。我就是这么想的:我想现在碰撞没有发生。控制台中是否未显示任何错误?如果答案是否定的,你确定你的砖块都有砖块脚本+BoxCollider吗?另一件要检查的事情是你的球是否有一个球体准直器+一个刚体组件?@Kardux没有错误。我检查了砖块,看起来我只给了家长砖块脚本,而孩子们没有脚本组件。大错特错!砖头现在被毁了,非常感谢你的帮助。很高兴我能帮忙!我把这个贴出来作为一个答案:自由选择:)祝你在发现和使用Unity方面玩得愉快代码>在类中调用?您能否尝试在本指令之前记录一些内容(Debug.log(“test”);
)?@Kardux感谢您的回复。我不熟悉unity,但根据我现在搜索的内容,控制台上应该会出现一个“测试”,对吗?但是什么也没发生。我就是这么想的:我想现在碰撞没有发生。控制台中是否未显示任何错误?如果答案是否定的,你确定你的砖块都有砖块脚本+BoxCollider吗?另一件要检查的事情是你的球是否有一个球体准直器+一个刚体组件?@Kardux没有错误。我检查了砖块,看起来我只给了家长砖块脚本,而孩子们没有脚本组件。大错特错!砖头现在被毁了,非常感谢你的帮助。很高兴我能帮忙!我将此作为一个答案发布:自由选择:)祝您在发现和使用Unity方面玩得愉快。
using UnityEngine; using System.Collections;
public class deathZone : MonoBehaviour {
void OnTriggerEnter(Collider col)
{
GM.instance.loseLife();
}
}
public class GM : MonoBehaviour {
public int Lives = 3;
public int bricks = 16;
public float resetDelay = 1f;
public Text livesText;
public GameObject gameOver;
public GameObject bricksPrefab;
public GameObject youWon;
public GameObject paddle;
public GameObject deathParticles;
public static GM instance = null;
private GameObject clonePaddle;
void Start()
{
if (instance == null)
instance = this;
else if (instance != this)
instance = null;
setup();
}
public void setup()
{
clonePaddle = Instantiate(paddle, new Vector3(0, -9,0), Quaternion.identity) as GameObject;
Instantiate(bricksPrefab, new Vector3((float)18.5, (float)-61.14095, (float)238.4855), Quaternion.identity);
}
void checkGameOver()
{
if (bricks < 1)
{
youWon.SetActive(true);
Time.timeScale = .25f;
Invoke("Reset", resetDelay);
}
if (Lives < 1)
{
gameOver.SetActive(true);
Time.timeScale = .25f;
Invoke("Reset", resetDelay);
}
}
void Reset()
{
Time.timeScale = 1f;
Application.LoadLevel(Application.loadedLevel);
}
public void loseLife()
{
Lives--;
livesText.text = "Lives: " + Lives;
Instantiate(deathParticles, clonePaddle.transform.position, Quaternion.identity);
Destroy(clonePaddle);
Invoke("SetupPaddle", resetDelay);
checkGameOver();
}
void SetupPaddle()
{
clonePaddle = Instantiate(paddle, new Vector3(0, -9, 0), Quaternion.identity) as GameObject;
}
public void destroyBrick()
{
bricks--;
checkGameOver();
}
}
public class Bricks : MonoBehaviour {
public GameObject brickParticle;
void OnCollisionEnter(Collision other)
{
Instantiate(brickParticle, transform.position, Quaternion.identity);
GM.instance.destroyBrick();
Destroy(gameObject);
}
}
public class deathZone : MonoBehaviour {
void OnTriggerEnter(Collider col)
{
GM.instance.loseLife();
}
}