So, I want this part named “Fist” found in the Workspace as seen below to orient to point in the direction the mouse is moving in (but only orient on the Z access, because my game is a two-dimensional based game)! Also note that the object is always following the mouse, remaining directly over it.
I’ve tried multiple different things, and have searched for questions and solutions on this topic, but have come up empty handed! I won’t post the things that I’ve tried, because none of them were anything I’d been looking for, and were pretty useless to me.
Check out the code! I’ve posted all the code that’s necessary to solving the problem.
while script.CanMove.Value == true do
local Target = game.Players.LocalPlayer:GetMouse().Hit
local x = Target.X
local y = Target.Y
local z = Target.Z
local Z_Value = -- So, what should this value be then? --
game.Workspace.Fist:MoveTo(Vector3.new(x,y,-73.5))
game.Workspace.Fist.FistU.Orientation = Vector3.new(0,0, Z_Value)
wait()
end
I’d greatly appreciate anyone’s hand in helping me solve this! It may be simple, but I don’t know the solution. Thanks!
if you need to keep part facing Z axis then u would need to change Y_Value.X and Z_Value.X to 0 and make sure Y_Value is perpendicular to Z_Value
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
while script.CanMove.Value do
local Part = game.Workspace.Fist
local Target = Mouse.Hit
local x = Target.X
local y = Target.Y
local z = Target.Z
local Y_Value = Target.YVector
local Z_Value = Target.ZVector
Part:MoveTo(Vector3.new(x,y,-73.5))
game.Workspace.Fist.FistU.CFrame = CFrame.fromMatrix(Part.FistU.Position, Vector3.new(),Y_Value, Z_Value)
task.wait() -- you can replace with RunService.Heartbeat:Wait()
end
1st step is to get the direction the mouse is moving in, otherwise known as mouse delta.
local lastHit = game.Players.LocalPlayer:GetMouse().Hit
while script.CanMove.Value == true do
local Target = game.Players.LocalPlayer:GetMouse().Hit
local delta = Target - lastHit --get the change in mouse position
local xyDelta = delta*Vector3.new(1,1,0) -- remove Z axis
lastHit = Target
--If there is no mouse delta (mouse is still replace it with a default direction)
--or last known direction your choice
if xyDelta.Magnitude <= 0.000001 then
xyDelta = Vector3.new(1,0,0)
end
game.Workspace.Fist:MoveTo(Vector3.new(x,y,-73.5))
local pos = game.Workspace.Fist.FistU.CFrame.Position
--Use CFrame.lookAt to make the fist point in that direction.
game.Workspace.Fist.FistU.CFrame = CFrame.lookAt(pos ,pos + xyDelta )
task.wait()
use
So, I tried to punch this in, but I got an error on your fifth line of code where we subtract Target from lastHit. The output error says “Expected Vector3, got CFrame”. So seeing this I put Target and lastHit in their own Vector3.new() functions, and I didn’t get an error when I loaded in, but the Fist is still, and instead of facing front, the way I need it to be facing, it’s rotated 90 degrees on the X axis.
I plugged in the code that you’ve sent to me, and… it’s complicated. Not the code, but what happened. I don’t get an error in the output, but it kinda glitches the whole Workspace. The Fist is invisible, but still there, and upon moving my mouse to the left side of the screen, half of all visible assets on the screen disappear, and when I move the mouse to the right side of the screen, the other half of the assets on the screen disappear.
Yeah, not sure what’s going on here. Thoughts on this though?
When the Orientation of the Fist is (0, 0, 0) for the X, Y, and Z axis respectively, the fist is upright, facing the direction I need it to. Manually changing the orientation of the Z property to whatever number will get the fist to change the direction it’s pointing in. That’s all I need done, but of course, I need my game to do this automatically.
If someone could just figure out what to change Z_Value to so the Fist is pointing in the direction the mouse is moving, that would simply be all I need.
Another method you could use is to use a 2D angle between vectors approach like here and use it for your Z angle.
Keep in mind you will still need to find and calculate mouse delta (change from position1 to position 2) and you cannot use input.Delta because the mouse isn’t locked.
Alright!
So, I’ve plugged this in, made some edits and worked out the kinks and it works well! Below I’ve attached the code if you’d like to see the finished product! Thanks a lot for the help! Couldn’t have done it without ya! : )
Before I wrap this discussion up, let’s say, hypothetically, I’d want to rotate on a different axis (being x or y) instead of the z. I’d assume I’d just change some of the variables here, but I don’t have time to test it out atm, and I want your input before I close the case here! So, what do you think?
local Target_2 = Mouse.Hit
while script.CanMove.Value == true do
local Target = Mouse.Hit
local Delta = Target.Position - Target_2.Position
local XYDelta = Delta*Vector3.new(1, 1, 0)
print(XYDelta)
Target_2 = Target
if XYDelta.Magnitude <= 0.000001 then
XYDelta = Vector3.new(1, 0, 0)
end
local x = Target.X
local y = Target.Y
local z = Target.Z
game.Workspace.Fist:MoveTo(Vector3.new(x,y,-73.5))
if Delta.X ~= 0 and Delta.Y ~= 0 then
game.Workspace.Fist.FistU.CFrame = CFrame.lookAt(game.Workspace.Fist.FistU.CFrame.Position, game.Workspace.Fist.FistU.CFrame.Position + XYDelta)
game.Workspace.Fist.FistU.CFrame *= CFrame.Angles(0,math.pi/2,80)
if Delta.X > 0 then
game.Workspace.Fist.FistU.Orientation = Vector3.new(game.Workspace.Fist.FistU.Orientation.X, 0, game.Workspace.Fist.FistU.Orientation.Z)
else
game.Workspace.Fist.FistU.Orientation = Vector3.new(game.Workspace.Fist.FistU.Orientation.X, 0, (360 - game.Workspace.Fist.FistU.Orientation.Z))
end
end
wait()
end