Jump to content

rotating a vehicle component smoothly


Recommended Posts

Posted

I've posted the same script sometime ago, but it turned out it wasn't the scripts fault, but rather a problem with the model.

Now I am wondering how I can smoothly rotate a vehicle component.

Right now when I press "u" the component instantly moves 90 degrees, but I'd much rather have that it rotates gradually. Much like in this video:

https://www.youtube.com/watch?v=Rg-BAa7gvuw&feature=youtu.be&t=45s

Here's my script:

function shamalFunctions()
    local vehicle = getPedOccupiedVehicle(localPlayer)
	local x, y, z = getVehicleComponents(vehicle, "gearflapleft" )
    if(vehicle)then
        if getElementModel(vehicle) == 519 then 
	        local rx, ry, rz = getVehicleComponentRotation(vehicle, "gearflapleft")
		    setVehicleComponentRotation(vehicle, "gearflapleft", rx, ry+90, rz)
		end	 
	end
end
bindKey("u", "down", shamalFunctions)

Thanks for your time :)

 

-Noah

Posted
function shamalFunctions()
    local vehicle = getPedOccupiedVehicle(localPlayer)
	local x, y, z = getVehicleComponents(vehicle, "gearflapleft" )
    if(vehicle)then
        if getElementModel(vehicle) == 519 then 
	        local rx, ry, rz = getVehicleComponentRotation(vehicle, "gearflapleft")
setTimer(function ()
		    setVehicleComponentRotation(vehicle, "gearflapleft", rx, ry+1, rz)
end,100,90)
		end	 
	end
end
bindKey("u", "down", shamalFunctions)

 

Posted (edited)
1 hour ago, Noah_Antilles said:

Thanks for replying @3laa33

I tried your script but it doesn't work, nothing happens when I press u

-- debug ?

Edited by 3laa33
Posted (edited)
4 minutes ago, IIYAMA said:

Update the variable:


ry

 

@Noah_Antilles follow this tutorial, because saying that something doesn't work, doesn't help fixing it at all.

 

i think there is something wrong with component name .. 

Edited by 3laa33
  • Moderators
Posted

Might be also possible.

But even if that is correct, not updating the ry variable will end up with the same result.

ry = ry +1

 

  • Like 1
  • Moderators
Posted (edited)

I am not sure about that at the moment, since you didn't even read it. A little shame on you. ;)

If you did read that topic, you would have known that waiting for warnings/errors will get you nowhere. (there is more to debugging)

 


You can update the ry variable like this: (make sure you put it inside the function which is called by the timer)

ry = ry + 1

 

Edited by IIYAMA
  • Moderators
Posted (edited)
setTimer(function ()
    -------------------
    -- place it here --
    -------------------
    
    --------------------
    -- and remove: +1 --
	setVehicleComponentRotation(vehicle, "gearflapleft", rx, ry --[[ +1]], rz) 
    --------------------
    
    -----------------
    -- debug it!!! --
    iprint("Debugging the variable ry:", ry)
    -----------------
end,100,90)

 

Edited by IIYAMA
  • Like 1
Posted

not tested

 

function shamalFunctions()
    vehicle = getPedOccupiedVehicle(localPlayer)
    local x, y, z = getVehicleComponents(vehicle, "gearflapleft" )
    if(vehicle)then
        if getElementModel(vehicle) == 519 and not moving then
            moving = true
            start = getTickCount()
            addEventHandler ( 'onClientRender', root, move)
        end  
    end
end
bindKey("u", "down", shamalFunctions)

function move ()
    _y = interpolateBetween(0,0,0,90,0,0,(getTickCount()-start)/1000,"Linear") 
    local rx, ry, rz = getVehicleComponentRotation(vehicle, "gearflapleft")
    setVehicleComponentRotation(vehicle, "gearflapleft", rx, ry+_y, rz)
    if _y == 90 then
        removeEventHandler ( 'onClientRender', root, move )
        moving = false
    end
end

 

Posted

@IIYAMA It finally works! thanks for your help. I however have one more question,

The opposite component also needs to rotate, but this time it needs to rotate -90 degrees instead of +90 degrees.

When I do this however I get the debugging error:

"expected positive, got negative" which makes me think that rotating components using a minus/negative value is not possible.

But it should be possible, some way or another.. right? 

  • Moderators
Posted (edited)
if ry < 0 then
  ry = 360 + ry
end

Try this. It will make sure that the rotation is positive and yet you can give it negative values. (use it directly after you updated the variable ry)

 

 

EDITED

Edited by IIYAMA
  • Like 1
Posted

@IIYAMA damn, this works perfectly aswell :D 

if you don't mind I have one last problem:

When I press "u"" again the components with negative values move another -90 degrees instead of rotating to their original location.

The components using a positive value don't share this problem, they work perfectly.

What can I do now? 

  • Moderators
Posted

You can do that by comparing the current position with the new position.

 

If the current position is 90 and you are moving to 90, you should do nothing.

If the current position is -90 and you are moving to -90, you should do nothing.

 

If the current position is -45 and you are moving to -90, then you should execute the timer not 90 times, but only 45 times.

end,100,90)

Calculated to:

end,100,45)

 

This is just doing the math, like you do on school.

 

I prefer onClientPreRender/onClientRender and getTickCount() for this kind of scripts, but I guess that is too complex for starters.

  • 5 years later...
Posted

@IIYAMA

On 6/17/2017 at 11:38 AM, Noah_Antilles said:

@IIYAMA damn, this works perfectly aswell :D 

if you don't mind I have one last problem:

When I press "u"" again the components with negative values move another -90 degrees instead of rotating to their original location.

The components using a positive value don't share this problem, they work perfectly.

What can I do now? 

I tried to create everything that was said, but I didn't understand anything they said

function shamalFunctions()
    local vehicle = getPedOccupiedVehicle(localPlayer)
	local x, y, z = getVehicleComponents(vehicle, "door_lf_dummy" )
    if(vehicle)then
        if getElementModel(vehicle) == 519 then 
	        local rx, ry, rz = getVehicleComponentRotation(vehicle, "door_lf_dummy")
setTimer(function (door_lf_dummy)
		    setVehicleComponentRotation(vehicle, "door_lf_dummy", rx, ry+1, rz)
end,100,90)
		end	 
	end
end
setTimer(function ()
    -------------------
    -- place it here --
    -------------------
    
    --------------------
    -- and remove: +1 --
	setVehicleComponentRotation(vehicle, "gearflapleft", rx, ry --[[ +1]], rz) 
    --------------------
    
    -----------------
    -- debug it!!! --
    iprint("Debugging the variable ry:", ry)
    -----------------
end,100,90)
bindKey("u", "down", shamalFunctions)

I know it's all wrong, but I wanted to understand how you use it to make other similar scripts

Posted (edited)
On 16/06/2017 at 23:10, Noah_Antilles said:

I've posted the same script sometime ago, but it turned out it wasn't the scripts fault, but rather a problem with the model.

Now I am wondering how I can smoothly rotate a vehicle component.

Right now when I press "u" the component instantly moves 90 degrees, but I'd much rather have that it rotates gradually. Much like in this video:

https://www.youtube.com/watch?v=Rg-BAa7gvuw&feature=youtu.be&t=45s

Here's my script:

function shamalFunctions()
    local vehicle = getPedOccupiedVehicle(localPlayer)
	local x, y, z = getVehicleComponents(vehicle, "gearflapleft" )
    if(vehicle)then
        if getElementModel(vehicle) == 519 then 
	        local rx, ry, rz = getVehicleComponentRotation(vehicle, "gearflapleft")
		    setVehicleComponentRotation(vehicle, "gearflapleft", rx, ry+90, rz)
		end	 
	end
end
bindKey("u", "down", shamalFunctions)

Thanks for your time :)

 

-Noah

local startTick = nil
local speedAnim = 4000

local enabled = false
local state = false

local curAnim = 0

function shamalFunctions()
    startTick = getTickCount()
    state = true
    enabled = not enabled
end
bindKey("u", "down", shamalFunctions)

addEventHandler ('onClientRender', root, function()
    local vehicle = getPedOccupiedVehicle(localPlayer)
    if not vehicle then return end
    if getElementModel(vehicle) ~= 519 then return end

    if not enabled then return end

    local easing = math.min(1, getEasingValue ((getTickCount()-startTick)/speedAnim, "Linear")

    if state then
        curAnim = easing
    else
        curAnim = 1 - easing
    end

    if curAnim <= 0.5 then
        local rx, ry, rz = getVehicleComponentRotation(vehicle, "gearflapleft")

        local newEasing = reMap (curAnim, 0, 0.5, 0, 1)

        setVehicleComponentRotation(vehicle, "gearflapleft", rx, 90*newEasing, rz)
    else
        local rx, ry, rz = getVehicleComponentRotation(vehicle, "gearflapleft")

        local newEasing = reMap (curAnim, 0.5, 1, 0, 1)

        setVehicleComponentRotation(vehicle, "gearflapleft", 90*newEasing, ry, rz)
    end
    
    if easing == 1 then
        enabled = false
    end
end)

function reMap(value, low1, high1, low2, high2)
    return low2 + (value - low1) * (high2 - low2) / (high1 - low1)
end

Testing this code

Edited by AngelAlpha
Posted
On 10/07/2022 at 05:59, AngelAlpha said:
local startTick = nil
local speedAnim = 4000

local enabled = false
local state = false

local curAnim = 0

function shamalFunctions()
    startTick = getTickCount()
    state = true
    enabled = not enabled
end
bindKey("u", "down", shamalFunctions)

addEventHandler ('onClientRender', root, function()
    local vehicle = getPedOccupiedVehicle(localPlayer)
    if not vehicle then return end
    if getElementModel(vehicle) ~= 519 then return end

    if not enabled then return end

    local easing = math.min(1, getEasingValue ((getTickCount()-startTick)/speedAnim, "Linear")

    if state then
        curAnim = easing
    else
        curAnim = 1 - easing
    end

    if curAnim <= 0.5 then
        local rx, ry, rz = getVehicleComponentRotation(vehicle, "gearflapleft")

        local newEasing = reMap (curAnim, 0, 0.5, 0, 1)

        setVehicleComponentRotation(vehicle, "gearflapleft", rx, 90*newEasing, rz)
    else
        local rx, ry, rz = getVehicleComponentRotation(vehicle, "gearflapleft")

        local newEasing = reMap (curAnim, 0.5, 1, 0, 1)

        setVehicleComponentRotation(vehicle, "gearflapleft", 90*newEasing, ry, rz)
    end
    
    if easing == 1 then
        enabled = false
    end
end)

function reMap(value, low1, high1, low2, high2)
    return low2 + (value - low1) * (high2 - low2) / (high1 - low1)
end

Testing this code

does not work

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...