#Recoil/Spring Recovery

1 messages · Page 1 of 1 (latest)

rocky glacier
#

Hey, im trying to implement a camera recoil by the use of springs. problem is that the recoil wont return back, its because im adding the output angles of the spring to the camera.CFrame

here what the code looks like

function module:step(dt)
  local cameracf = camera.CFrame
  local camerarecoil = self.springs.camerarecoil:update(dt)
  
  camera.CFrame = camera.CFrame  * CFrame.Angles(math.rad(camerarecoil.X), math.rad(camerarecoil.Y), math.rad(camerarecoil.Z))
end

is there any work arounds to this?
or if there's a way to get the BaseCF for the camera (as in the absoulte output of the camera from the CoreScripts before it is set to the cameras CFrame)

haughty garnetBOT
#

studio** You are now Level 2! **studio

rocky glacier
#

heres the spring module i am using

-- Constants

local ITERATIONS = 16
local SPRING = {}

-- Functions 

function SPRING.create(self, mass, force, damping, speed)

    local spring    = {
        Target        = Vector3.new();
        Position    = Vector3.new();
        Velocity    = Vector3.new();

        Mass        = mass or 5;
        Force        = force or 50;
        Damping        = damping or 4;
        Speed        = speed  or 4;
    }

    function spring:shove(force)
        local x, y, z    = force.X, force.Y, force.Z
        if x ~= x or x == math.huge or x == -math.huge then
            x    = 0
        end
        if y ~= y or y == math.huge or y == -math.huge then
            y    = 0
        end
        if z ~= z or z == math.huge or z == -math.huge then
            z    = 0
        end
        self.Velocity = self.Velocity + Vector3.new(x, y, z)
    end

    function spring:update(dt) : Vector3
        local scaledDeltaTime = dt * self.Speed / ITERATIONS

        for i = 1, ITERATIONS do
            local iterationForce = self.Target - self.Position
            local acceleration = (iterationForce * self.Force) / self.Mass

            acceleration = acceleration - self.Velocity * self.Damping

            self.Velocity = self.Velocity + acceleration * scaledDeltaTime
            self.Position = self.Position + self.Velocity * scaledDeltaTime
        end

        return self.Position
    end

    return spring
end

-- Return

return SPRING
jovial geyser
#

Reduce the force of the spring. I bet u that most games that uses recoil don’t fix the issue that ur describing, but they just reduce the force to prevent the camera to be uncontrollable. I’ve played games like Fortnite where the recoil doesn’t make their camera stay in place. It’s just the player that has to adjust accordingly to the recoil.

#

But if u do want the camera to return back, all u gotta do is do task.wait() and then apply the negative of camera recoil

rocky glacier
rocky glacier
#

but i did finally managed to find my own solution, which is by reverse-engineering the playermodule and fork the cameramodules to store a value called "BaseCameraCFrame", which is the output of the cframe before it is actually set to the camera

#

and gave me the results i needed

#

thank you for trying to help me tho @jovial geyser