Hello I'm having an issue with my colliders registering. My two prefabs collide with each other and the environment but I am trying to program my game so that when the enemy touches the player, the player loses damage but there is no lowering in damage occurring. My debug logs show that the computer isn't even registering the collider at all. Any help is appreciated thanks! I have rigidbodies on both prefabs as well as capsule and box colliders.
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);
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…