using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;

public class CameraController : MonoBehaviour
{
    public float mad = 1;//狂暴度
    public float delayTime = 0.2f;//执行时间
    //public float nexttoTime;

    private bool isHit;
    private Vector3 currentTran;//记录的初始位置

    
    //private float nextAttackTime;//下次攻击时间
    
    private void Update()
    {
        if (isHit)
        {
            transform.position = (Random.insideUnitSphere * mad) + currentTran;
        }
    }

    /// <summary>
    /// 玩家受伤
    /// </summary>
    public void Player_Hit()
    {
        //if (nextAttackTime < Time.time)
        //{
          //  return;//不进行攻击
           // nextAttackTime = nexttoTime + Time.time;
        //}
        currentTran = transform.position;
        isHit = true;
        StartCoroutine(Move());
    }

    /// <summary>
    /// 晃动 计时器,防止卡住
    /// </summary>
    /// <returns></returns>
    IEnumerator Move()
    {
        Time.timeScale = 0f;
        yield return new WaitForSecondsRealtime(delayTime);
        Time.timeScale = 1f;
        isHit = false;
        transform.position = currentTran;
    }
}

使用random,以及timeScale做出来的震动效果,可配合其他api实现更好的效果!

Last modification:February 18th, 2022 at 02:46 pm
如果觉得我的文章对你有用,请随意赞赏