[英]Collider not registering on prefab
您好,我的对撞机注册有问题。 我的两个预制件相互碰撞并与环境发生碰撞,但我正在尝试对我的游戏进行编程,以便当敌人接触玩家时,玩家会失去伤害,但伤害不会降低。 我的调试日志显示计算机甚至根本没有注册对撞机。 感谢任何帮助。 我在预制件以及胶囊和盒子对撞机上都有刚体。
using UnityEngine;
using System.Collections;
using Mirror;
using UnityEngine.UI;
public class Player : NetworkBehaviour
{
CharacterController characterController;
public float speed = 6.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public int maxHealth = 30;
public int currentHealth;
public GameObject myPrefab;
public int damage = 10;
Transform cameraTransform;
private GambeObject HealthBar = Find;
private Vector3 moveDirection = Vector3.zero;
void Start()
{
myPrefab.transform.Rotate(0.0f ,0.0f ,0.0f );
Instantiate(myPrefab, new Vector3(0, 0, 5), Quaternion.identity);
characterController = GetComponent<CharacterController>();
currentHealth = maxHealth;
FindObjectOfType<HealthBar>().SetMaxHealth(maxHealth);
// FindObjectOfType<HealthBar>().SetMaxHealth(maxHealth);
// cameraTransform = Camera.main.transform;
}
void TakeDamage(int damage) {
currentHealth -= damage;
FindObjectOfType<HealthBar>().SetHealth(currentHealth);
// FindObjectOfType<HealthBar>().SetHealth(currentHealth);
}
void OnCollisionEnter(Collision col){
Debug.Log("collision statement");
if(col.gameObject.tag == "Enemy"){
currentHealth-=damage;
Debug.Log("Snowman Collision detected");
}
}
void Update()
{
if (isLocalPlayer){
if (characterController.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f,
Input.GetAxis("Vertical"));
moveDirection *= speed;
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
characterController.Move(moveDirection * Time.deltaTime);
transform.rotation = Quaternion.LookRotation(moveDirection);
}
}
public override void OnStartLocalPlayer()
{
Camera.main.GetComponent<CameraFollow>().setTarget(gameObject.transform);
}
}
您是否仔细检查了编辑器中的角色碰撞器设置? 取消选中IsTrigger 。 小心设置对撞机。
Collider.OnTriggerEnter注意:两个游戏对象都必须包含一个 Collider 组件。 必须启用 Collider.isTrigger,并包含刚体。 如果两个游戏对象都启用了 Collider.isTrigger,则不会发生碰撞。 当两个游戏对象都没有刚体组件时,同样适用。
检查您是否缺少某些东西。 根据你的代码,我猜你错过了重要的一点。 你的雪人对撞机不会触发,它是一个敌方角色,与世界互动而不经过它,这也适用于你的角色。 然后你应该使用Collider.OnCollisionEnter
我终于想通了; 我需要将“currentHealth -= damage”行更改为“TakeDamage(10):”非常感谢大家的帮助:D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.