|
| 04 May 2011 09:38 PM |
I've been working on a turret that is remote-controlled by the player's WASD keys. A motor is activated for every respective key, making the turret rotate and tilt. There is, however, a problem. Whenever I try to stop movement, letting go of the key, the turret continues to wobble up and down or back and forth, depending on which axis I was rotating it on. This is a huge problem, as it destroys all precision. Here is the script with the motor functions. Pardon me for the long lines, but roblox doesn't accept variables for ParamB values.
Tool = script.Parent Player = game.Players.LocalPlayer TopSpeed = .1
function onEquipped(mouse) Tool.CamScript.Disabled = false Tool.NewTargetter.Disabled = false keyDownConn = mouse.KeyDown:connect(onKeyDown) keyUpConn = mouse.KeyUp:connect(onKeyUp) end
function onKeyDown(key) if key then print("Key Pressed") if script.Parent.IsDriving.Value == true then local theKey = string.lower(key) if (theKey == "w") then print("W Pressed") script.Parent.Tracer.Value.Parent.TurretParts.Angler2.FrontParamB = script.Parent.Tracer.Value.Parent.TurretParts.Angler2.FrontParamB - TopSpeed print("Pointing Up") elseif (theKey == "s") then print("S Pressed") script.Parent.Tracer.Value.Parent.TurretParts.Angler2.FrontParamB = script.Parent.Tracer.Value.Parent.TurretParts.Angler2.FrontParamB + TopSpeed print("Pointing Down") elseif (theKey == "d") then print("D Pressed") script.Parent.Tracer.Value.Parent.TurretParts.Angler1.BottomParamB = script.Parent.Tracer.Value.Parent.TurretParts.Angler1.BottomParamB - TopSpeed print("Pointing Left") elseif (theKey == "a") then print("A Pressed") script.Parent.Tracer.Value.Parent.TurretParts.Angler1.BottomParamB = script.Parent.Tracer.Value.Parent.TurretParts.Angler1.BottomParamB + TopSpeed print("Pointing Right") end end end end
function onKeyUp(upKey) if upKey then local theKey = string.lower(upKey) if (theKey == "w") then print("W Released") script.Parent.Tracer.Value.Parent.TurretParts.Angler2.FrontParamB = script.Parent.Tracer.Value.Parent.TurretParts.Angler2.FrontParamB + TopSpeed print("Up Stop") elseif (theKey == "s") then print("S Released") script.Parent.Tracer.Value.Parent.TurretParts.Angler2.FrontParamB = script.Parent.Tracer.Value.Parent.TurretParts.Angler2.FrontParamB - TopSpeed print("Down Stop") elseif (theKey == "d") then print("D Released") script.Parent.Tracer.Value.Parent.TurretParts.Angler1.BottomParamB = script.Parent.Tracer.Value.Parent.TurretParts.Angler1.BottomParamB + TopSpeed print("Left Stop") elseif (theKey == "a") then print("A Released") script.Parent.Tracer.Value.Parent.TurretParts.Angler1.BottomParamB = script.Parent.Tracer.Value.Parent.TurretParts.Angler1.BottomParamB - TopSpeed print("Right Stop") end end end
function onUnequipped(mouse) keyDownConn:disconnect() keyUpConn:disconnect() Tool.CamScript.Disabled = true Tool.NewTargetter.Disabled = true end
Tool.Equipped:connect(onEquipped)
Tool.Unequipped:connect(onUnequipped)
It appears roblox doesn't like my long script lines, so some lines, namely the lines defining and modding the ParamB stuff, overlap to the next one or two lines. My apologies. I do not know which part of this script could be malfunctioning, as no special output is given, and I've looked over this many times. |
|
|
| Report Abuse |
|
|
|
| 04 May 2011 10:04 PM |
| Alright, so I figured out how to stop the wobbling. However, now the barrel of the turret no longer defies gravity. :( Instead of adding/subtracting the values when the user released the key, I just set it so 0. No wobbles anymore, but now I need to make this thing defy gravity. :I |
|
|
| Report Abuse |
|
|
|
| 04 May 2011 10:14 PM |
Anchor it and use CFrame?
Or you could use BodyPosition and BodyGyro. |
|
|
| Report Abuse |
|
|
|
| 04 May 2011 10:19 PM |
| I want this turret to be able to move laterally on top of a tank eventually. I haven't currently found a way to make bodygyro and bodyposition work perfectly, where the turret can move up & down at any rotation. If you can provide such a way, please inform me, so I can fix this thing up! :D |
|
|
| Report Abuse |
|
|
|
| 04 May 2011 10:24 PM |
| In that case, you could weld it to the tank, then modify the weld's rotation when the player presses a key. |
|
|
| Report Abuse |
|
|
|
| 04 May 2011 10:35 PM |
| Yikes, I hate welds like that. Is there any possible way to get a motor to counteract gravity? |
|
|
| Report Abuse |
|
|
|
| 04 May 2011 10:37 PM |
You could add a BodyForce to counteract gravity.
Or you could use a BodyPosition to keep it at a constant elevation. |
|
|
| Report Abuse |
|
|
|
| 04 May 2011 10:42 PM |
| I had an idea. Could I make it so that the script makes a weld when the turret isn't moving up/down so that it stays the same relative to the turret base? When it is moved up and down again, it'll delete the weld, then make a new one when it is finished. |
|
|
| Report Abuse |
|
|
|
| 04 May 2011 10:50 PM |
I suppose that could work.
But you can also get vertical movement through manipulation of the weld's coordinateframe alone. |
|
|
| Report Abuse |
|
|
|
| 04 May 2011 10:54 PM |
| I may look into that if my other solution doesn't work. Testing right now. :) |
|
|
| Report Abuse |
|
|
|
| 04 May 2011 10:58 PM |
| It worked! Thank you for your help, allofthese! I really appreciate you taking your time to help me! |
|
|
| Report Abuse |
|
|