Noah_Antilles Posted June 16, 2017 Share Posted June 16, 2017 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 Link to comment
AE. Posted June 16, 2017 Share Posted June 16, 2017 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) Link to comment
Noah_Antilles Posted June 17, 2017 Author Share Posted June 17, 2017 Thanks for replying @3laa33 I tried your script but it doesn't work, nothing happens when I press u Link to comment
AE. Posted June 17, 2017 Share Posted June 17, 2017 (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 June 17, 2017 by 3laa33 Link to comment
Moderators IIYAMA Posted June 17, 2017 Moderators Share Posted June 17, 2017 Update the variable: ry @Noah_Antilles follow this tutorial, because saying that something doesn't work, doesn't help fixing it at all. Link to comment
AE. Posted June 17, 2017 Share Posted June 17, 2017 (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 June 17, 2017 by 3laa33 Link to comment
Moderators IIYAMA Posted June 17, 2017 Moderators Share Posted June 17, 2017 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 1 Link to comment
Noah_Antilles Posted June 17, 2017 Author Share Posted June 17, 2017 @IIYAMA I'm not that incompetent. debugging didn't give any error, hence why I simply told @3laa33 that it doesn't work. The component's name is correct, I double checked it. How do I update the ry value? Link to comment
Moderators IIYAMA Posted June 17, 2017 Moderators Share Posted June 17, 2017 (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 June 17, 2017 by IIYAMA Link to comment
AE. Posted June 17, 2017 Share Posted June 17, 2017 i checked the shamal components names and there is no such component with this name ! Link to comment
Noah_Antilles Posted June 17, 2017 Author Share Posted June 17, 2017 @3laa33 It's a custom component I named that way. It works because it used to move with my old script. @IIYAMA I tried putting it inside the function. It works for half a second, and then stops again. Where do I need to put it exactly? Link to comment
Moderators IIYAMA Posted June 17, 2017 Moderators Share Posted June 17, 2017 (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 June 17, 2017 by IIYAMA 1 Link to comment
Simple. Posted June 17, 2017 Share Posted June 17, 2017 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 Link to comment
Noah_Antilles Posted June 17, 2017 Author Share Posted June 17, 2017 @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? Link to comment
Moderators IIYAMA Posted June 17, 2017 Moderators Share Posted June 17, 2017 (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 June 17, 2017 by IIYAMA 1 Link to comment
Noah_Antilles Posted June 17, 2017 Author Share Posted June 17, 2017 @IIYAMA damn, this works perfectly aswell 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? Link to comment
Moderators IIYAMA Posted June 17, 2017 Moderators Share Posted June 17, 2017 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. Link to comment
copo Posted July 9, 2022 Share Posted July 9, 2022 @IIYAMA On 6/17/2017 at 11:38 AM, Noah_Antilles said: @IIYAMA damn, this works perfectly aswell 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 Link to comment
AngelAlpha Posted July 10, 2022 Share Posted July 10, 2022 (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 July 10, 2022 by AngelAlpha Link to comment
copo Posted July 14, 2022 Share Posted July 14, 2022 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 Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now