2020. 12. 24. 01:31
Unity/C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapBorder : MonoBehaviour {
private void OnDrawGizmos()
{
Gizmos.DrawWireCube(borderCenter, borderSize);
}
public enum DropOption
{
None
, Respawn
, Destroy
}
public DropOption dropOption = DropOption.Respawn;
public Vector3 borderCenter = Vector3.zero;
public Vector3 borderSize = new Vector3(200,100,200);
public Transform respawnPoint;
Vector3 firstPosition;
Quaternion firstRotation;
// Use this for initialization
void Start ()
{
firstPosition = transform.position;
firstRotation = transform.rotation;
}
// Update is called once per frame
void Update () {
var pos = transform.position;
pos.x = Mathf.Clamp(pos.x, borderCenter.x - borderSize.x, borderCenter.x + borderSize.x );
pos.y = Mathf.Min(pos.y, borderCenter.y + borderSize.y );
pos.z = Mathf.Clamp(pos.z, borderCenter.z - borderSize.z , borderCenter.z + borderSize.z);
transform.position = pos;
if (transform.position.y<borderCenter.y- borderSize.y )
{
switch (dropOption)
{
case DropOption.None:
break;
case DropOption.Respawn:
if (respawnPoint == null)
{
transform.position = firstPosition;
transform.rotation = firstRotation;
}
else
{
transform.position = respawnPoint.transform.position;
transform.rotation = respawnPoint.transform.rotation;
}
break;
case DropOption.Destroy:
Destroy(gameObject);
break;
default:
break;
}
}
}
}