Alright i'll start with my overall Objective first to provide some Insight into what i'm attempting to do. As of right now i'm trying to design a Tank. The issue is that it's a one person tank. This means i'm not going to two have two separate control modules. I want the player piloting the tank to be able to both steer and drive the tank as-well as Operate the turret System. So here is the issue at hand. I'm looking for a way to link a players mouse position to the turret system. The idea i have as of now is that moving the horizontal position of the mouse (Relative to the player) will rotate the Turret itself on Y axis and that moving your mouse Vertically will Rotate the Turret's Barrel on t he X Axis. And while i have designed something that can achieve this task, it operates based off of the activation of the Steer and Drive functions of a vehicle seat. So the real question here: Is how would i go about linking the rotation of an object on the X axis and another object on the Y axis based on the location of the player's mouse on the screen?
(Please note that the tank still need to move, So Anchored objects aren't exactly Preferable here.)
I also have looked into things such as UnitRays but can't quite figure out how to link that to an objects Rotation.
Seriously, Thanks for the help. ~DeadMann
It is really pretty easy, just do the following:
game.Players.PlayerAdded:connect(function(p) -- when a player joins local mouse = p:GetMouse() -- get's their mouse local part = Instance.new("Part",game.Workspace) -- makes a new part part.Position = mouse.Hit + Vector3.new(0,0.5,0) -- spawns the part where you are pointing your mouse print(mouse.Target.Name) -- prints the name of the object the mouse is pointing at end)
To explain:
Line 4:
mouse.Hit
is the Vector3
(3D) value of where the mouse is pointing at in the game.
E.G, the position of mouse.Hit
is Vector3.new(5,5,5)
.
Line 5:
mouse.Target
is the object that the mouse is pointing at, it is nil if there is no object.
E.G, print(mouse.Target.Name)
would print > Baseplate (as long as the mouse is on Baseplate
).
To apply it:
To do what you want to do, it would be recommended to weld all of the parts that are part of the 'gun' for the tank. Welds make sure that if one part moves, the others stay in the same position relative to the part they are welded to. Wiki article: http://wiki.roblox.com/index.php/Weld
Next, you need to use CFrame
to rotate an object according to a 3D position.
CFrame has 2 arguments:
Argument 1:
Position where you want the part that you are CFraming to move to. Vector3 value.
Argument 2:
What position the part is going to be looking at, so you can rotate objects to face another object.
Wiki article: http://wiki.roblox.com/index.php?title=Cframe
So, all you have to do is CFrame the part that is welded to all of the other parts in the 'gun'. Like so:
-- i haven't defined anything, this is just how to do it while usingGun == true then -- makes sure the player is actually using the gun wait() mainPart.CFrame = CFrame.new(mainPart.Position,mouse.Hit) -- cframes the gun end
Hope I helped :)