
1.制作障碍系统 (1.制作几种类型的障碍物的预制体 ((*.编写障碍物基类,以下几种均继承自基类,含碰撞体,RememberY(生成障碍物时该有多大的Y) ((1.矮障碍,仅碰撞体 ((2.高障碍,仅碰撞体 ((3.可冲破障碍 (((1.除基本碰撞体外,额外包含一个触发器,比碰撞体先检测到马,同时获取马的x速度,大于阈值则给障碍做破碎,用马的力度决定破碎力,关闭碎块和马的碰撞 (((*.导入某2D破碎插件 ((4.人马分离障碍 (((WAIT,需要等待人马分离系统先搭建 (2.编写障碍物生成系统 ((1.每若干时间,生成一个随机一种障碍,若干的范围可控 (((1.设计协程,从预制体列表中随机出一种,并在计算好的位置实例化,随后等待范围内的若干时间,然后检查马的存活情况,若马仍存活,重新调用本协程 ((2.生成的位置:x在相机右侧若干不变距离,y根据障碍物的不同而不同,需要计算保存。 (3.编写障碍物消亡系统 ((1.每个障碍物和碎片都会在离开镜头后被删除
66 lines
2.3 KiB
C#
66 lines
2.3 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
|
|
|
|
public class ExplosionForce : MonoBehaviour {
|
|
public float force = 50;
|
|
public float radius = 5;
|
|
public float upliftModifer = 5;
|
|
|
|
/// <summary>
|
|
/// create an explosion force
|
|
/// </summary>
|
|
/// <param name="position">location of the explosion</param>
|
|
public void doExplosion(Vector3 position){
|
|
transform.localPosition = position;
|
|
StartCoroutine(waitAndExplode());
|
|
}
|
|
|
|
/// <summary>
|
|
/// exerts an explosion force on all rigidbodies within the given radius
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private IEnumerator waitAndExplode(){
|
|
yield return new WaitForFixedUpdate();
|
|
|
|
Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position,radius);
|
|
|
|
foreach(Collider2D coll in colliders){
|
|
if(coll.GetComponent<Rigidbody2D>()&&coll.name!="hero"){
|
|
//Roman Update
|
|
//使其仅影响碎片
|
|
if(coll.gameObject.layer == LayerMask.NameToLayer("Fragment"))
|
|
//
|
|
AddExplosionForce(coll.GetComponent<Rigidbody2D>(), force, transform.position, radius, upliftModifer);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// adds explosion force to given rigidbody
|
|
/// </summary>
|
|
/// <param name="body">rigidbody to add force to</param>
|
|
/// <param name="explosionForce">base force of explosion</param>
|
|
/// <param name="explosionPosition">location of the explosion source</param>
|
|
/// <param name="explosionRadius">radius of explosion effect</param>
|
|
/// <param name="upliftModifier">factor of additional upward force</param>
|
|
private void AddExplosionForce(Rigidbody2D body, float explosionForce, Vector3 explosionPosition, float explosionRadius, float upliftModifier = 0)
|
|
{
|
|
var dir = (body.transform.position - explosionPosition);
|
|
float wearoff = 1 - (dir.magnitude / explosionRadius);
|
|
Vector3 baseForce = dir.normalized * explosionForce * wearoff;
|
|
baseForce.z = 0;
|
|
body.AddForce(baseForce);
|
|
|
|
if (upliftModifer != 0)
|
|
{
|
|
float upliftWearoff = 1 - upliftModifier / explosionRadius;
|
|
Vector3 upliftForce = Vector2.up * explosionForce * upliftWearoff;
|
|
upliftForce.z = 0;
|
|
body.AddForce(upliftForce);
|
|
}
|
|
|
|
}
|
|
}
|