I’m working on a Mars Rover game where you control a rover and explore the surface. I’m currently using two hinge constraints to rotate the rover’s “head” up/down and left/right.
Problem:
The problem I’m facing is when I disable one of the hinges so the other can move, the head snaps back to where the hinge would be at TargetAngle 0. If I don’t disable one of the hinge constraints when rotating the other, then neither of them rotate. What should I do to fix this? Any and all help is appreciated!
Here’s a video showing my problem:
The way the hinges rotate on a different axis than the other is because the attachments’ orientations are different.
Here’s the script that rotates them:
local remote = game.ReplicatedStorage.KeyEvent
local debounce = false
local head = script.Parent.Head
local boom = script.Parent.Base["Meshes/14"]
local pressed = false
remote.OnServerEvent:Connect(function(player,button)
local chara = workspace.Rover
if chara then
local human = chara:FindFirstChildWhichIsA("Humanoid")
if button == "R" then
if not pressed then
pressed = true
head["Meshes/11"].HingeConstraint.Enabled = true
head["Meshes/8"].HingeConstraint2.Enabled = false
repeat
head["Meshes/11"].HingeConstraint.TargetAngle = head["Meshes/11"].HingeConstraint.TargetAngle - 0.3
wait()
until pressed == false
end
elseif button == "F" then
if not pressed then
pressed = true
head["Meshes/11"].HingeConstraint.Enabled = true
head["Meshes/8"].HingeConstraint2.Enabled = false
repeat
head["Meshes/11"].HingeConstraint.TargetAngle = head["Meshes/11"].HingeConstraint.TargetAngle + 0.3
wait()
until pressed == false
end
elseif button == "Q" then
if not pressed then
pressed = true
pressed = true
head["Meshes/8"].HingeConstraint2.Enabled = true
head["Meshes/11"].HingeConstraint.Enabled = false
repeat
head["Meshes/8"].HingeConstraint2.TargetAngle = head["Meshes/8"].HingeConstraint2.TargetAngle - 0.3
wait()
until pressed == false
end
elseif button == "E" then
if not pressed then
pressed = true
pressed = true
head["Meshes/8"].HingeConstraint2.Enabled = true
head["Meshes/11"].HingeConstraint.Enabled = false
repeat
head["Meshes/8"].HingeConstraint2.TargetAngle = head["Meshes/8"].HingeConstraint2.TargetAngle + 0.3
wait()
until pressed == false
end
end
end
end)
And here’s the other half of the script that stops the rotation:
game.ReplicatedStorage.EndEvent.OnServerEvent:Connect(function(player,button)
local chara = workspace.Rover
if chara then
local human = chara:FindFirstChildWhichIsA("Humanoid")
if human then
if button == "R" then
pressed = false
head["Meshes/11"].HingeConstraint.TargetAngle = head["Meshes/11"].HingeConstraint.TargetAngle
elseif button == "F" then
pressed = false
head["Meshes/11"].HingeConstraint.TargetAngle = head["Meshes/11"].HingeConstraint.TargetAngle
elseif button == "Q" then
pressed = false
head["Meshes/8"].HingeConstraint2.TargetAngle = head["Meshes/8"].HingeConstraint2.TargetAngle
elseif button == "E" then
pressed = false
head["Meshes/8"].HingeConstraint2.TargetAngle = head["Meshes/8"].HingeConstraint2.TargetAngle
end
end
end
end)
Quick summary of how the script works currently:
It enables the rotation on one axis by enabling the hinge constraint that rotates on that axis, and disables (sets enabled to false) the hinge constraint for the other axis, which is why it snaps back to default angle.
Why disable the hinge constraints in the first place? If you need the hinges to be locked to the rover, you could create welds and enable/ disable them.
I see the issue you are having since you want the two hinges to work together… have you looked into or tried to use a universal constraint?
Read this doc page and it might help with having your rover head on one universal constraint, which allows for two perpendicular axes working with each other:
Awesome, thanks! I’m having some trouble though finding anywhere that actually exactly explains in basic lame-man’s terms how to use it, lol. Would you mind, pointing me to some place that can help me understand it better, or give me a brief description on how I could use it along with the hinges?
Here is a very user friendly and clear explanation of how to use the constraint and i believe you won’t even have a need to keep the two hinge constraints… just control the one universal constraint through your existing script:
Hope this helps and would love to see the finished product when you’re done!
I’m having a little difficulty understanding how to control the constraint via script. I see it doesn’t have any properties like hinges, so how would I control it’s angle via the script? Since the head of the rover is controllable via keybinds.
You can maybe then control the max angle, which moves in a cone shape to the main attachment’s axis. I even think you can do this with script you dont necessarily have to create an instance but i could be wrong. I would try out it but i can’t get to computer until tomorrow.
Try to play around with the max angle and see if you can control its limits by tying them to key bind.
If all this is too much and you dont want to go the route of universal, try using the two hinges and instead of creating them physically… create instances of them instead and when you need to go up and down it creates the instance of that hinge and when it has to go left and right you destroy that instance and create one that will go left and right… it will always snap back to center but thats ok cuz thats robotic, but atleast it wont disable one hinge.
Ah alright. I won’t be able to get to computer until tomorrow either, but I’ll see what I can do then. I’m curious, how would I use the maxangle to get it to rotate on each specific axis for the key binds?
But there isn’t a way to do it without the snapping. I really want to eliminate the snapping back somehow because right now the whole purpose of it being able to rotate is kind of defeated if it can only do one axis.
No i meant you create an instance of the hinge constraint for vertical movement and when it needs to look horizontally, you destroy the instance of the vertical you created and create the horizontal. In the middle of those two, the head will always be centered but thats ok cuz robots look up or down then center before they look left and right ( well old school robots atleast lol).
Im just wondering how to script the instances because your robot moves location. That will be an area where the scripters can chime in, i know basics but not creating hinge instances on moving objects.
After thinking a little about it, that makes much more sense now. Thanks so much, I think it might work! I’ll try it tomorrow morning. I appreciate your help! The old school robotic style would probably work best for right now. Maybe I’ll have a go at more complicated stuff when I have more experience and understand it more.
I’m sorry if I missed it, but do you have issues with CanCollide Parts hitting each other causing the head to snap back to center?
Also, hinges usually need quite a bit of Force, try upping it from 5000 to 1000000 to see if that makes a difference.
So I just spent some time trying this, but I still have the same problem. Whenever I delete the old hinge, it snaps back. I’ve tried rotating the attachments to the orientation of the head whenever the hinge finishes rotating, but it still doesn’t work.