碰撞检测问题
Issues on collision detection
不确定为什么我的碰撞没有导致我的控制台打印出来 "i hit enemy"。玩家有刚体组件,敌人没有。
我的敌人有 tag
个敌人。敌人正在使用变换移动。我的玩家有刚体组件,我的敌人没有。有任何想法吗?
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float moveSpeed;
public float maxSpeed = 5f;
private Vector3 input;
private Rigidbody rb;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
if (rb.velocity.magnitude < maxSpeed) {
rb.AddForce(input * moveSpeed);
}
}
void onCollisionEnter(Collision other)
{
if (other.transform.tag == "Enemy")
{
print ("I hit enemy");
}
}
}
可能有多种解决方案。也许你只是忘了给你的敌人设置标签?
请查看 Unity3D 碰撞矩阵(https://docs.unity3d.com/Manual/CollidersOverview.html - 在页面底部)。
碰撞矩阵会告诉您何时会收到碰撞消息。例如。您不能让静态对撞机与刚体对撞机发生碰撞。重新检查所有游戏对象。它们是否符合碰撞的需要?
Unity 新用户的另一个简单错误。拼写很重要!只需将 onCollisionEnter
替换为 OnCollisionEnter
。回调函数区分大小写,它们的第一个字母通常是大写。
如果更改此设置不起作用,请将 Rigidbody 也附加到您的敌人。确保它们都连接了 Colliders,并且 IsTrigger 不是 enabled
.
不确定为什么我的碰撞没有导致我的控制台打印出来 "i hit enemy"。玩家有刚体组件,敌人没有。
我的敌人有 tag
个敌人。敌人正在使用变换移动。我的玩家有刚体组件,我的敌人没有。有任何想法吗?
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float moveSpeed;
public float maxSpeed = 5f;
private Vector3 input;
private Rigidbody rb;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update () {
input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
if (rb.velocity.magnitude < maxSpeed) {
rb.AddForce(input * moveSpeed);
}
}
void onCollisionEnter(Collision other)
{
if (other.transform.tag == "Enemy")
{
print ("I hit enemy");
}
}
}
可能有多种解决方案。也许你只是忘了给你的敌人设置标签? 请查看 Unity3D 碰撞矩阵(https://docs.unity3d.com/Manual/CollidersOverview.html - 在页面底部)。
碰撞矩阵会告诉您何时会收到碰撞消息。例如。您不能让静态对撞机与刚体对撞机发生碰撞。重新检查所有游戏对象。它们是否符合碰撞的需要?
Unity 新用户的另一个简单错误。拼写很重要!只需将 onCollisionEnter
替换为 OnCollisionEnter
。回调函数区分大小写,它们的第一个字母通常是大写。
如果更改此设置不起作用,请将 Rigidbody 也附加到您的敌人。确保它们都连接了 Colliders,并且 IsTrigger 不是 enabled
.