使用Unity实现时光倒流

首先声明,博主Unity版本为5.4.4

在游戏中加入时光逆流可以衍生出很多有趣的事,独立游戏《时空幻境》里面就有这个功能,这篇文章就是模拟时光逆流


首先简单的搭建一下场景

使用Unity实现时光倒流_第1张图片

这里就是拿cube堆出了大方块

使用Unity实现时光倒流_第2张图片

所有的cube都添加刚体,脚本后面附上

使用Unity实现时光倒流_第3张图片

把bomb摆到cube中间


上代码:

using UnityEngine;
using System.Collections;

public class Bomb : MonoBehaviour {

    public float radius = 5.0f;//爆炸半径
    public float power = 10;//爆炸力
    public float explosiveLift = 1.0f;//从下方多少距离施加力


    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown (KeyCode.Space))
            Fire ();
    }

    private void Fire(){
        Vector3 pos = transform.position;
        Collider[] colliders = Physics.OverlapSphere (pos,radius);
        foreach (var hit in colliders) {
            if (hit.GetComponent ()) {
                hit.GetComponent ().AddExplosionForce (power,pos,radius,explosiveLift);
            }
        }
    }
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TimeBack : MonoBehaviour {

    public bool isRewinding = false;//用来判断是否需要时光逆流
    public float recordTime;//时光逆流时间

    private List informations;
    private Rigidbody rb;

    // Use this for initialization
    void Start () {
        rb = GetComponent ();
        informations = new List ();
    }

    // Update is called once per frame
    void Update () {
        //按下shift开始时光倒流
        if (Input.GetKeyDown (KeyCode.LeftShift)) {
            StartRewind ();
        }
        //松开时停止
        if (Input.GetKeyUp (KeyCode.LeftShift)) {
            StopRewind ();
        }
    }

    void FixedUpdate(){
        if (isRewinding)
            Rewind ();
        else
            Record ();

    }

    /// 
    /// 开始时光逆流
    /// 
    private void StartRewind(){
        isRewinding = true;
        rb.isKinematic = true;//使物体不受力
    }

    /// 
    /// 停止时光逆流
    /// 
    private void StopRewind(){
        isRewinding = false;
        rb.isKinematic = false;//物体开始受力
    }

    /// 
    /// 时光逆流
    /// 
    private void Rewind(){
        //记录点数量大于0时才可以倒流
        if (informations.Count >0) {
            Information information = informations [0];
            transform.position = information.position;
            transform.rotation = information.rotation;
            informations.RemoveAt (0);
        }
    }

    /// 
    /// 记录物体的信息
    /// 
    private void Record(){
        if (informations.Count > Mathf.Round (recordTime / Time.fixedDeltaTime)) {
            informations.RemoveAt (informations.Count-1);
        }
        informations.Insert (0,new Information(transform.position,transform.rotation));
    }
}
using System;
using UnityEngine;

/// 
/// 记录物体的位置跟旋转信息,用于实现时光倒流的技能
/// 
public class Information{
    public Vector3 position;
    public Quaternion rotation;
    public Information (Vector3 _position,Quaternion _rotation)
    {
        position = _position;
        rotation = _rotation;
    }
}

最后的效果的话,博主不会录gif所以,喜欢的自己去试吧

你可能感兴趣的:(Unity)