generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: ways to interrupt a lerp in progress

Previous Thread :: Next Thread 
robocu3 is not online. robocu3
Joined: 13 Mar 2009
Total Posts: 6485
18 Jun 2015 11:27 PM
lets say we have a discrete lerp method

local RenderSteppedEvent = game:GetService("RunService").RenderStepped

local function Lerp(duration, func)
local d = 0
local start = tick()
local current = start
local function step()
current = tick()
d = current - start
if d >= duration then func(1) return false end
return func(d / duration)
end
while true do
if not step() then break end
RenderSteppedEvent:wait()
end
end

and we wanted a function to interpolate the camera's FOV.

local Camera = workspace.CurrentCamera

local IsFOVTransitioning = false --if the FOV is currently transitioning this will be true.
local DoInterrupt = false --this will be true if we're trying to interrupt an
--interpolation in progress.

local function SetFOV(int, duration)
if IsFOVTransitioning then --if the FOV is currently changing, interrupt it.
DoInterrupt = true
repeat RenderSteppedEvent:wait() until not DoInterrupt
end
local start = Camera.FieldOfView
local change = int - start
if not change ~= 0 then --insure we're not transitioning to the same number
IsFOVTransitioning = true --make sure we acknowledge that we're transitioning here.
duration = duration or .1
local WasInterrupted
Lerp(duration, function(t) --attempt to lerp
local CurrentFOV = start + (change * t)
Camera.FieldOfView = CurrentFOV
if DoInterrupt then DoInterrupt = false; WasInterrupted = true; return false end
--if do interrupt is not nil/false, set it to that, acknowledge the
--interruption, and stop the lerp
end)
if not WasInterrupted then
IsFOVTransitioning = false
--if we weren't interrupted, the FOV should be done interpolating.
end
end
end

the problem here is that we need it to interrupt if it's midway through the interpolation and a mousebutton2 input is received, and we were prepared for that; the lerp function checks if step returns nil or false, if it does it breaks immediately.
so we set up binds and what not.

local function AttemptFOVTransition(_, State)
if State.Name == "Begin" then
SetFOV(10)
else
SetFOV(70)
end
end

local ContextActionService = game:GetService("ContextActionService")

ContextActionService:BindAction("FOVTransition", AttemptFOVTransition, false, Enum.UserInputType.MouseButton2)

It works fine. You can test the code yourself.
The problem I'm having is that if you tap right mouse fast enough, it stops working. I can't really tell why.

tl;dr
get off my post if you're not gonna read it, rude

-=Robo=-
Report Abuse
robocu3 is not online. robocu3
Joined: 13 Mar 2009
Total Posts: 6485
18 Jun 2015 11:30 PM
correction in the lerp method;

local function Lerp(duration, func)
local d = 0
local start = tick()
local current = start
local function step()
current = tick()
d = current - start
if d >= duration then func(1) return false end
return func(d / duration) ~= false --this was wrong lol
end
while true do
if not step() then break end
RenderSteppedEvent:wait()
end
end

-=Robo=-
Report Abuse
cody123454321 is not online. cody123454321
Joined: 21 Nov 2009
Total Posts: 5408
18 Jun 2015 11:31 PM
I would create an environment to Lerp in.

local Lerp = {}

Lerp.new = function()
local this = {}
this.Object = nil
this.Locked = false
this.Execute = nil --

this.Tick = function(self
-- Now you can check if locked and do your updates here
if(not self.Locked)then
self.Execute()
end
end

this.ToggleLock = function(self)
self.Locked = not self.Locked
end

return this
end

return Lerp
Report Abuse
robocu3 is not online. robocu3
Joined: 13 Mar 2009
Total Posts: 6485
18 Jun 2015 11:39 PM
Are you sure that'd help here? I appreciate the suggestion, but I'm just kind of stuck as to why it just stops executing.
I'm assuming it's where it repeats a wait until DoInterrupt is false, but I'm not sure why DoInterrupt isn't being set to false?

-=Robo=-
Report Abuse
cody123454321 is not online. cody123454321
Joined: 21 Nov 2009
Total Posts: 5408
18 Jun 2015 11:40 PM
print() things until you run into the error.
Report Abuse
robocu3 is not online. robocu3
Joined: 13 Mar 2009
Total Posts: 6485
18 Jun 2015 11:45 PM
I did just now, it's definitely DoInterrupt not being set to false.
Now I just need to figure out why that's the case and how I can make it better. lol

-=Robo=-
Report Abuse
robocu3 is not online. robocu3
Joined: 13 Mar 2009
Total Posts: 6485
18 Jun 2015 11:57 PM
bump

-=Robo=-
Report Abuse
cody123454321 is not online. cody123454321
Joined: 21 Nov 2009
Total Posts: 5408
19 Jun 2015 12:02 AM
Set DoInterrupt to global scope and you win.
Report Abuse
robocu3 is not online. robocu3
Joined: 13 Mar 2009
Total Posts: 6485
19 Jun 2015 12:05 AM
DoInterrupt is in the global scope...?

-=Robo=-
Report Abuse
robocu3 is not online. robocu3
Joined: 13 Mar 2009
Total Posts: 6485
19 Jun 2015 12:05 AM
I put a local out of force of habit. lol

-=Robo=-
Report Abuse
robocu3 is not online. robocu3
Joined: 13 Mar 2009
Total Posts: 6485
19 Jun 2015 12:18 AM
bumpp

-=Robo=-
Report Abuse
robocu3 is not online. robocu3
Joined: 13 Mar 2009
Total Posts: 6485
19 Jun 2015 01:30 AM
salty salt

-=Robo=-
Report Abuse
CloneTrooper1019 is not online. CloneTrooper1019
Joined: 19 Jan 2009
Total Posts: 6227
19 Jun 2015 08:55 AM
Yeah, you're doing it the hard way.
You should be using what I like to call "Value Gliding"
------------------------------------------------------------------------------------------

local c = workspace.CurrentCamera
local rs = game:GetService("RunService")
local contextAction = game:GetService("ContextActionService")

local currentGoal = 70
local zoomRate = 5
local down = false

local function glideTorwards(current,goal,rate)
if current < goal then
return math.min(current + rate, goal)
elseif current > goal then
return math.max(current - rate, goal)
else
return goal
end
end

local function update()
local fovGoal = down and 10 or 70
c.FieldOfView = glideTorwards(c.FieldOfView,fovGoal,zoomRate)
end

local function onMouse2Down(_,state)
down = (state.Name == "Begin")
end

contextAction:BindAction("FOVTransition",onMouse2Down,false,Enum.UserInputType.MouseButton2)
rs.RenderStepped:connect(update)
----------------------------------------------------------------------------------------
Report Abuse
robocu3 is not online. robocu3
Joined: 13 Mar 2009
Total Posts: 6485
19 Jun 2015 06:55 PM
thx clone

-=Robo=-
Report Abuse
A2D8 is not online. A2D8
Joined: 15 Jun 2014
Total Posts: 548
19 Jun 2015 07:23 PM
Oh god, what? Why would you do that? Linear interpolation is perfectly fine.
Report Abuse
mycheeze is not online. mycheeze
Joined: 27 Jun 2011
Total Posts: 6748
19 Jun 2015 07:24 PM
Why interpolate when we already have like 50 modules/plugins that do this already
Report Abuse
robocu3 is not online. robocu3
Joined: 13 Mar 2009
Total Posts: 6485
19 Jun 2015 11:39 PM
I have no idea what you're talking about mycheeze. lol
And why not? Clone's is more reliable and doesn't get stuck because of variable problems.

-=Robo=-
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image