程式碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TrapHarm: MonoBehaviour {
public GameObject harmObject;
public float damageAmount = 10f;
public float damageTimeInterval=1f;
private float contactTime = 0f;
private bool isContact=false;
void Update () {
if (isContact)
contactTime += Time.deltaTime;
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == harmObject.name)
{
isContact = true;
harmObject.GetComponent<PlayerHP>().TakeDamage(damageAmount);
}
}
private void OnCollisionStay(Collision collision)
{
if ((collision.gameObject.name == harmObject.name) && (contactTime >= damageTimeInterval))
{
harmObject.GetComponent<PlayerHP>().TakeDamage(damageAmount);
contactTime = 0;
}
}
private void OnCollisionExit(Collision collision)
{
if (collision.gameObject.name == harmObject.name)
{
isContact = false;
contactTime = 0;
}
}
}
沒有留言:
張貼留言