问题描述
您好,我的对撞机注册有问题。我的两个预制件相互碰撞并与环境发生碰撞,但我正在尝试对我的游戏进行编程,以便当敌人接触玩家时,玩家会损失伤害但不会降低伤害发生。我的调试日志显示计算机根本没有注册对撞机。任何帮助表示赞赏谢谢!我在预制件以及胶囊和盒子碰撞器上都有刚体。
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,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"),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.isTrigger,并包含刚体。如果两个游戏对象都启用了 Collider.isTrigger,则不会发生碰撞。当两个 GameObjects 都没有 Rigidbody 组件时,同样适用。
检查您是否缺少某些东西。根据你的代码,我猜你错过了重要的一点。你的雪人对撞机不会触发,它是一个敌人角色并且与世界互动而不进入它,这也适用于你的角色。那么你应该使用 Collider.OnCollisionEnter
,我终于明白了!我需要将“currentHealth -= damage”行更改为“TakeDamage(10);”非常感谢大家的帮助:D