Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

c# - Collider not registering on prefab

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);
   }

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Did you check your character collider settings in editor carefully? Uncheck IsTrigger. Setup collider carefully.

Collider.OnTriggerEnter Note: Both GameObjects must contain a Collider component. One must have Collider.isTrigger enabled, and contain a Rigidbody. If both GameObjects have Collider.isTrigger enabled, no collision happens. The same applies when both GameObjects do not have a Rigidbody component.

Check if you have something missing. According to your code I guess you missing IMPORTANT point. Your snowman collider not trigger, it is a enemy character and interact with world without passing in it, also this applies to your character too. Then you should use Collider.OnCollisionEnter


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...