unity 中lua实现震屏

Posted 絮叨ing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity 中lua实现震屏相关的知识,希望对你有一定的参考价值。

--[[

    shake_power 震动强度

    frequency 震动频率

    shake_time 震动时间

    start_shake_time 开始往前拉伸镜头时间

    recovery_time 摄像机恢复时间

    stretch_length 摄像机每次前后拉伸长度

    shake_angle 震动角度

]]

function startShake( self, shake_power, frequency, shake_time,start_shake_time,recovery_time,stretch_length,shake_angle)

   local T = 1 / frequency

if shake_angle == 0 then

shake_angle = math.random(0, 360)

self._phaseX = math.random(0, 1)*T

self._phaseY = math.random(0, 1)*T

self._phaseZ = math.random(0, 1)*T

else

self._phaseX = T

self._phaseY = T

self._phaseZ = T

end

if CameraUtils.curCameraType == GameCameraType.Camera_3D then

    local amount = _hShake*shake_power + _vShake*shake_power

    self._shakeAmount = Quaternion.AngleAxis(shake_angle, Vector3.up) * amount

else

    local amount = _h2DShake*shake_power

    self._shakeAmount = Quaternion.AngleAxis(shake_angle, Vector3.forward) * amount

    end

    if recovery_time == nil or recovery_time > shake_time then

        recovery_time = shake_time

    end

    if start_shake_time == nil or start_shake_time > recovery_time then

        start_shake_time = recovery_time

    end

    if stretch_length == nil or stretch_length == 0 then

        stretch_length = 0.2

    end

    local camera = CameraUtils.GetCurCamera()

    if self.fieldOfViewBool then

        self.fieldOfViewBool = false

        self.fieldOfView = camera.fieldOfView

    end

    camera.fieldOfView = self.fieldOfView

    if self._shakeTimer == 0 then

        self.oldCameraPos = camera.transform.position

    else

        camera.transform.position = self.oldCameraPos

    end

self._shakeTime = shake_time

self._shakeTimer = shake_time

    self._shakeFrequency = frequency

    self.start_shake_time = start_shake_time

    self._recovery_time = recovery_time

    self._stretch_length = stretch_length

    self._isInShake = true

end

 

function endShake( self )

    self._isInShake = false

    self._shakeTimer = 0

    self._recovery_time = 0

    self.start_shake_time = 0

    self._shakeOffset = Vector3.zero

    self._stretch_length = 0

    local camera = CameraUtils.GetCurCamera()

    if self.fieldOfView then

        camera.fieldOfView = self.fieldOfView

    end

    if self.oldCameraPos ~= nil then

        camera.transform.position = self.oldCameraPos

    end

end

 

local function triFun( t, a, T )

    local tt = t - math.floor(t / T) * T

return 2 * a * math.abs(1 - 2 / T * tt) - a

end

 

local function UpdateShake( self, elapsedTime )

    if self._isInShake == false then return end

 

    local falloff = self._shakeTimer / self._shakeTime

    self._shakeTimer = self._shakeTimer - elapsedTime

    self._recovery_time = self._recovery_time - elapsedTime

    self.start_shake_time = self.start_shake_time - elapsedTime

    if self._shakeTimer > 0 then

local amountX = self._shakeAmount.x * falloff

local amountY = self._shakeAmount.y * falloff

local amountZ = self._shakeAmount.z * falloff

local time = self._shakeTimer

local shtX = time + self._phaseX

local shtY = time + self._phaseY

local shtZ = time + self._phaseZ

local T = 1 / self._shakeFrequency

local am = self._shakeAmount.magnitude * falloff * 0.5

        local camera = CameraUtils.GetCurCamera()

self._shakeOffset = Vector3(triFun(shtX, amountX, T), triFun(shtY, amountY, T), triFun(shtZ, amountZ, T)) + camera.gameObject.transform.forward * triFun(time, am, T)

        local auto_gen_vector3 = camera.gameObject.transform.position + self._shakeOffset

        GameObjectHelper.position( camera.gameObject, auto_gen_vector3.x, auto_gen_vector3.y, auto_gen_vector3.z)

        if self._recovery_time < 0 and self._recovery_time + elapsedTime >= 0 then

            camera.fieldOfView = self.fieldOfView

        else if self.start_shake_time <= 0 and self._recovery_time >= 0 and camera.fieldOfView >= 12 then

            camera.fieldOfView = camera.fieldOfView - self._stretch_length

        end

        end

    else

self:endShake()

end

end


以上是关于unity 中lua实现震屏的主要内容,如果未能解决你的问题,请参考以下文章

Unity中C#与Lua的交互

游戏开发实战手把手教你在Unity中使用lua实现红点系统(前缀树 | 数据结构 | 设计模式 | 算法 | 含工程源码)

游戏开发实战手把手教你在Unity中使用lua实现红点系统(前缀树 | 数据结构 | 设计模式 | 算法 | 含工程源码)

Unity3D热更新之LuaFramework篇[06]--Lua中是怎么实现脚本生命周期的

unity怎么通过lua加载一个游戏对象

cocos-lua是怎么实现场景的切换的