How to change the Y axis of a part/model while a tween is happening

Right now I have a custom rig which I want to script similarily to an npc walking randomly around the place. The problem is the map has a ton of slopes, so if I were to tween from let’s say the part’s position which is at the lower end of the slope, to a random point on the higher end of the slope then the rig will look like it’s floating mid air. I’ve tried to solve this issue by also raycasting to the ground while the tween is happening so that the rig appears to be constantly on the ground while the tween plays but changing the Y axis while the tween plays out doesn’t work. Any solutions please?

use lerping instead of tweening, the only issue is that you can only use linear or it gets complicated

This is a Post I made a few hours ago, change it to fit your need:

Lerp function

local function Lerp(a, b, t)
	return a + (b - a) * t
end

function for tweening

local OverallTime = 0

local function Tween(Part, DeltaTime, NewPosition, OldPosition, TimeForTween, Name)
    OverallTime += DeltaTime
    if OverallTime >= TimeForTween then
        Part.Position = NewPosition
        RunService:UnBindRenderToStep(Name)
    end
    Part.Position = Lerp(OldPosition, NewPosition, OverallTime/ TimeForTween)
end

local function Tween1(DeltaTime)
    Tween(
        workspace.Part, ---Part or value
        DeltaTime. ---The delta Time
        Vector3.new(0, 10, 0) --- Position To Tween To
        Vector3.new(Part.Position.X, 0, Part.Position.Z) -- the old position of the part
        10 --The time for the tween
        "Tween1" --Name of the tween
    )
end

local function Tween2()
    Tween(
        workspace.Part, ---Part or value
        DeltaTime. ---The delta Time
        Vector3.new(0, -5, 0) --- Position To Tween To
        Vector3.new(Part.Position.X, 0, Part.Position.Z) -- the old position of the part
        2 --The time for the tween
        "Tween2" --Name of the tween
    )
end

RunService:BindRenderToStep("Tween1", 0, Tween1)

task.wait(2.5)

RunService:BindRenderToStep("Tween2", 0, Tween2)
1 Like

I completely forgot lerping exists, works perfectly thank you a ton!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.