Prop类
public Vector3[] leftTargetPosition; // 道具做抛物线运动的目标位置的数组(左侧)
public Vector3[] rightTargetPosition; // 道具做抛物线运动的目标位置的数组(右侧)
public Vector3[] middleTargetPosition; // 道具做抛物线运动的目标位置的数组(中间)
private void Awake()
{
// 将指定位置存入数组
rightTargetPosition = new Vector3[]
{
new Vector3(13f, 6f, 9f),new Vector3(15f,6f,0f)
};
leftTargetPosition = new Vector3[]
{
new Vector3(-5f, 6f, 9f),new Vector3(-8f, 6f, 0f)
};
middleTargetPosition = new Vector3[]
{
new Vector3(2f, 6f, 9f),new Vector3(6f, 6f, 10f)
};
}
// Player类中,当Player与两个路标道具相撞,调用此方法
// 如果Player在跑道左侧 则道具被撞飞到跑道左侧
// 如果Player在跑道右侧 则道具被撞飞到跑道右侧
// 如果Player在中间 则道具被撞飞到前面
public void collideSign(Vector3[] TargetPosition)
{
transform.GetComponent<BoxCollider>().enabled = false;
Vector3 targetPos = TargetPosition[UnityEngine.Random.Range(0, TargetPosition.Length)];
JumpTo(targetPos);
}
public void JumpTo(Vector3 targetPos)
{
// 抛物线运动效果
transform.DOMove(targetPos, 2f).OnComplete(() => GameObject.Destroy(this.gameObject));
// 持续旋转效果
transform.DORotate(new Vector3(360, 360, 360), 2f, RotateMode.FastBeyond360).SetLoops(-1);
}
调用时,写在Player类的碰撞检测方法中:
private void OnTriggerEnter(Collider other)
{
var propObj = other.transform.gameObject.GetComponent<PropObj>(); // 获取碰撞物体所挂载的道具类
if (propObj == null) return; // 确保碰撞到的只会是挂载了道具类的物体(道具)
var prop = propObj.pType; // 获取道具类中的枚举类型
switch (prop)
{
case PropType.ConeObject: // 锥形路标
if (GetDirecton() == MoveDirection.Left)
{
propObj.collideSign(propObj.leftTargetPosition);
}
else if (GetDirecton() == MoveDirection.Right)
{
propObj.collideSign(propObj.rightTargetPosition);
}
else
{
propObj.collideSign(propObj.middleTargetPosition);
}
break;
case PropType.WarningObject: // 警示路标
if (GetDirecton() == MoveDirection.Left)
{
propObj.collideSign(propObj.leftTargetPosition);
}
else if (GetDirecton() == MoveDirection.Right)
{
propObj.collideSign(propObj.rightTargetPosition);
}
else
{
propObj.collideSign(propObj.middleTargetPosition);
}
break;
}
}