我觉得这将有助于说明我的目标与这个脚本是什么。我试图使火箭着陆游戏。你开始在一个垫上,基本上有一个控制下降,直到你的土地-玩家可以左键点击激活推进器移动在屏幕上的鼠标的方向。(我没有问题的推进器/物理运动在游戏中...只是碰撞检测。)
此特定脚本旨在:
1.检测碰撞
1.特别检测碰撞的物体。(绿色、黄色、红色着陆垫或地面)
1.一旦碰撞发生,立即停止所有物理和运动。(把它想象成游戏/应用程序中的背景。一旦你着陆,一切都冻结,游戏管理器将显示一个窗口,显示你的分数,和2个按钮重试或退出。
在“Rocket”上有一个刚体,但是“Rocket”是一个空对象,它是“Thruster”的父对象,而“Thruster”是“Rocket Body”的父对象。
Rocket(Has Rigidbody,no collider)-〉Thruster(No Rigidbody,has box collider)-〉-〉Rocket Body(No Rigidbody,has box collider)
其他信息。刚体是动态的,不是运动学的,在这个场景中没有什么“IsTrigger”意味着任何着陆垫或地面---尽管我试图让它们都触发,看看它是否会记录碰撞。
我知道我想把火箭的刚体设置为在任何碰撞发生的时刻都是运动学的。我只是不明白为什么我甚至不能检测到碰撞,尽管火箭有一个RB。
这是代码--我对自己的编码能力不是很有信心,所以我倾向于尝试所有我能做的事情,并在后端修剪脂肪,所以我绝对愿意接受我错过了一些非常基本的东西的想法。如果你看到的只是看起来像一个混乱的东西,我提前道歉。也很抱歉,但是发送一个截图太多了,但不足以证明2。
下面的代码-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LandingBehaviour : MonoBehaviour
{
[SerializeField]
private bool _greenPad;
[SerializeField]
private bool _yellowPad;
[SerializeField]
private bool _redPad;
[SerializeField]
private bool _ground;
private bool _crash = false;
private float _score = 1;
[SerializeField]
private Rigidbody _rigidBody;
private void Awake()
{
_rigidBody = GetComponent<Rigidbody>();
}
private void OnCollisionEnter(Collision collision)
{
Debug.Log("A collision has occurred.");
//_rigidBody = GetComponent<Rigidbody>();
_rigidBody.isKinematic = false;
if (!collision.gameObject.CompareTag("LandingPad") || !collision.gameObject.CompareTag("Ground"))
{
return;
}
if (collision.gameObject.transform.rotation.z >= 45 || collision.gameObject.transform.rotation.z <= -45)
{
_crash = true;
}
if (collision.gameObject.CompareTag("LandingPad"))
{
if (_greenPad)
{
Debug.Log("Green Landing Pad Found.");
_score *= 2;
}
if (_yellowPad)
{
Debug.Log("Yellow Landing Pad Found.");
_score *= 3;
}
if (_redPad)
{
Debug.Log("Red Landing Pad Found.");
_score *= 5;
}
}
if (collision.gameObject.CompareTag("Ground"))
{
Debug.Log("Ground Found.");
_score *= 1;
}
}
}
1条答案
按热度按时间cngwdvgl1#
确保火箭和垫都有碰撞器没有被标记为触发器,并且标签被正确地放置在实际有碰撞器的物体中