接上一回,想了想打算做一个仿照节奏光剑的游戏,先是花了一晚上找了找解决方案,有一个Openpose还有一个ThreeDPoseTracker,看了看配置好麻烦,而且都太吃配置了,我的1060还想多用几年,所以最后还是买了个Kinect

这篇探究下怎么去实现物体切割,用到了ezy-slice这个插件https://github.com/DavidArayan/ezy-slice

简单的切割

首先摆好场景,一个Plane和两个Cube

然后给Slicer添加代码,并把Cube设为Slice

using System.Collections;
using System.Collections.Generic;
using EzySlice;
using UnityEngine;
public class Slicer : MonoBehaviour {
    void Start () {

    }
    void Update () {
        Collider[] colliders = Physics.OverlapBox (transform.position, transform.localScale / 2, transform.rotation, LayerMask.GetMask ("Slice"));
        if (Input.GetKeyDown (KeyCode.D)) {
            foreach (var collider in colliders) {
                Destroy (collider.gameObject);
                //位置与法线获得切割面
                collider.gameObject.SliceInstantiate (transform.position, transform.up);
            }
        }
    }
}

这样当SlicerCube接触并按下D键,就会切割物体

还可以给生成的切块施加一个方向的力,这样就不会一起掉落

Collider[] colliders = Physics.OverlapBox (transform.position, transform.localScale / 2, transform.rotation, LayerMask.GetMask ("Slice"));
if (colliders.Length > 0) {
    foreach (var collider in colliders) {
        Destroy (collider.gameObject);
        //位置与法线获得切割面
        GameObject[] objs = collider.gameObject.SliceInstantiate (transform.position, transform.up);
        //添加刚体并设为凸多面体
        foreach (var obj in objs) {
            obj.gameObject.AddComponent<Rigidbody> ();
            obj.gameObject.AddComponent<MeshCollider> ().convex = true;
        }
        //施加力
        objs[0].gameObject.GetComponent<Rigidbody> ().AddForce (transform.up * 100);
        objs[1].gameObject.GetComponent<Rigidbody> ().AddForce (-transform.up * 100);
    }
}

给切割面添加材质

public Material sliceSurface;
//省略
Collider[] colliders = Physics.OverlapBox (transform.position, transform.localScale / 2, transform.rotation, LayerMask.GetMask ("Slice"));
if (colliders.Length > 0) {
    foreach (var collider in colliders) {
        Destroy (collider.gameObject);
        //位置与法线获得切割面
        SlicedHull hull = collider.gameObject.Slice (transform.position, transform.up);
        if (hull != null) {
            GameObject upper = hull.CreateLowerHull (collider.gameObject, sliceSurface);
            GameObject lower = hull.CreateUpperHull (collider.gameObject, sliceSurface);
            GameObject[] objs = new GameObject[] { upper, lower };

            foreach (var obj in objs) {
                //添加刚体并设为凸多面体
                obj.gameObject.AddComponent<Rigidbody> ();
                obj.gameObject.AddComponent<MeshCollider> ().convex = true;
            }
            //施加力
            objs[0].gameObject.GetComponent<Rigidbody> ().AddForce (transform.up * 100);
            objs[1].gameObject.GetComponent<Rigidbody> ().AddForce (-transform.up * 100);
        }
    }
}

使用DoTween制作动画

节奏光剑的物块都是从玩家面前出现的,所以先简单地做一个物体从前方出现并移动到玩家面前的动画

using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
public class CubeManager : MonoBehaviour {
    public GameObject cubePb;
    public Transform[] originTrans;
    public Transform[] targetTrans;
    void Start () {
        InvokeRepeating ("creatCube", 0, 2f);
    }
    void Update () {

    }
    void creatCube () {
        int random = Random.Range (0, 2);
        GameObject gb = Instantiate (cubePb, originTrans[random].position, cubePb.transform.rotation);
        gb.transform.DOMove (targetTrans[random].position, 5).OnComplete (delegate () {
            //播放完动画后销毁
            Destroy (gb);
        });
    }
}

效果

美化

再使用这个插件做一个简易的光剑

https://assetstore.unity.com/packages/tools/particles-effects/volumetric-lines-29160

还可以添加切割特效

Last modification:March 16th, 2021 at 11:10 pm