[英]Collision with enemy to inflict damage
I'm having some problems with Unity. 我在Unity方面遇到了一些问题。 I'm trying to make it so that if an enemy collides with the player, the player loses a health point.
我正在努力做到,如果敌人与玩家发生碰撞,玩家就会失去生命值。 My C# code is below.
我的C#代码如下。
before you look at the code, I wanted to say that the enemies are rigid bodies so that the object bullets can affect them. 在您看代码之前,我想说一下敌人是刚体,所以物体的子弹会影响他们。 I made an extra capsule to be a part of the player body that can be a rigid body so that the code can detect the collision.
我制作了一个额外的囊,作为玩家身体的一部分,可以是刚体,以便代码可以检测到碰撞。 Do you think that would work?
您认为这样行得通吗? I'm unsure if it's easier for a rigid body to detect another rigid-body collision or if it doesn't care.
我不确定刚体是否更容易检测到另一个刚体碰撞或是否无关紧要。
public class playerhealth : MonoBehaviour {
private int curHealth;
private int playerLives;
public GUIText winText;
public GUIText healthText;
public GUIText livesText;
void Start() {
curHealth = 3;
playerLives = 3;
SetHealthText();
SetLivesText();
winText.text = "";
}
void FixedUpdate()
{
// where physics codes go
}
// HERE'S WHERE THE COLLISIONS STUFF IS
void OnCollisionEnter(Collider rigidbody) {
if (rigidbody.gameObject.tag == "Enemy") {
curHealth = curHealth - 1;
SetHealthText();
}
if (rigidbody.gameObject.tag == "reloader") {
playerLives = playerLives - 1;
SetLivesText();
}
}
// setting GUI TEXT and reloading level
void SetHealthText() {
healthText.text = "Health Points: " + curHealth.ToString();
if (curHealth <= 0) {
Application.LoadLevel("shootingworld");
playerLives = playerLives - 1;
}
if(curHealth >= 10) {
playerLives+= 1;
}
}
void SetLivesText() {
livesText.text = "Lives: " + playerLives.ToString();
if (playerLives <= 0) {
winText.text = "GAME OVER";
}
}
}
You're making a number of assumptions here, some of which are wrong. 您在这里做出了许多假设,其中一些是错误的。 I'll try to point them out.
我会尝试指出它们。
Adding a RigidBody to a gameobject is the right idea, but it's the Collider component that determines the shape and size of the object's collision. 向游戏对象添加RigidBody是正确的主意,但是由Collider组件确定对象碰撞的形状和大小。 Consider adding a BoxCollider, SphereCollider or CapsuleCollider to both.
考虑将BoxCollider,SphereCollider或CapsuleCollider添加到两者中。
I assume you're having trouble getting the objects to actually collide, this may be the solution. 我认为您在使对象实际碰撞方面遇到麻烦,这可能是解决方案。
Also, 也,
void OnCollisionEnter(Collider rigidbody){
The Collision class contains information about contact points, impact velocity etc.
Collision类包含有关接触点,冲击速度等的信息。
Anyway you've got the right idea regarding comparing a Collider's gameobject by tag. 无论如何,您都有关于按标签比较对撞机的游戏对象的正确想法。 All you need to do is enforce the tag on the object, either in the editor or through code.
您需要做的就是在编辑器中或通过代码在对象上强制标记。
You are using this: 您正在使用此:
void OnCollisionEnter(Collider collision) {
} }
Collider is used for Ontrigger... 对撞机用于Ontrigger ...
Try this: 尝试这个:
void OnCollisionEnter(Collision collision) {
} }
Hope this help ! 希望对您有所帮助! :)
:)
You try to this may help full also 您尝试这样做可能也有助于全面
void OnCollisionEnter(Collision collision)
{
}
Documentation is : 文档是:
http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.