Hi, I’m making a TIE shooter (from star wars). Basically a ship. I was wondering how I could make the model ship face in the direction of the player mouse. And when W is pressed, head towards where it’s faced.
This is an image of the ship: https://i.gyazo.com/f98ae6907f139e9e759633feb987b3ea.png. I want the black dot in the middle with the two red dots (lights) to be facing where the players mouse is.
Oh is it a top down view game?
-- get the user input service
local userInputService = game:GetService("UserInputService")
-- get the mouse location on screen
local mouse = userInputService:GetMouseLocation()
-- convert the 2d position to 3d position
local ray = workspace.CurrentCamera:ViewportPointToRay(mouse.X, mouse.Y)
-- print the position and direction
print(ray.Origin, ray.Direction)
-- the position we want the part to be
local position = ray.Origin + ray.Direction * 10
-- use the position and direction to make a cframe
part.CFrame = CFrame.LookAt(position, position + ray.Direction)
Camera is CurrentCamera, right?
Yes it would be. Current Camera’s name says it all.
It doesn’t work. It doesn’t follow my mouse
I’m using a localscript for the script
i guess your not always positioning it but just doing it once
you need to use RunService.Heartbeat to update the position every frame
local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")
local camera = workspace.CurrentCamera
-- run this function every frame
runService.Heartbeat:Connect(function(deltaTime)
local mouse = userInputService:GetMouseLocation()
local ray = camera:ViewportPointToRay(mouse.X, mouse.Y)
local position = ray.Origin + ray.Direction * 10
part.CFrame = CFrame.LookAt(position, position + ray.Direction)
end)
local userInputService = game:GetService("UserInputService")
local mouse = userInputService:GetMouseLocation()
local ray = camera:ViewportPointToRay(mouse.X, mouse.Y)
print(ray.Origin, ray.Direction)
local position = ray.Origin + ray.Direction * 10
script.Parent.Parent.CFrame = CFrame.LookAt(position, position + ray.Direction)
local runService = game:GetService("RunService")
local userInputService = game:GetService("UserInputService")
local camera = workspace.CurrentCamera
-- run this function every frame
runService.Heartbeat:Connect(function(deltaTime)
local mouse = userInputService:GetMouseLocation()
local ray = camera:ViewportPointToRay(mouse.X, mouse.Y)
local position = ray.Origin + ray.Direction * 10
script.Parent.Parent.CFrame = CFrame.LookAt(position, position + ray.Direction)
end)
This is my current script
that code is messed up why are you getting the userinputservie twice?
and why are you updating the cframe outside the loop?
I don’t know, lol. I’m also getting camera twice
don’t just copy and paste the code read it and try to understand it
here are some comments
-- get the run service
local runService = game:GetService("RunService")
-- get the userinput service
local userInputService = game:GetService("UserInputService")
-- get the current camera
local camera = workspace.CurrentCamera
-- run this function every frame (60 times a second)
runService.Heartbeat:Connect(function(deltaTime)
-- get the mouse position in 2D
local mouse = userInputService:GetMouseLocation()
-- convert the mouse from 2D to 3D
local ray = camera:ViewportPointToRay(mouse.X, mouse.Y)
-- make a position using the mouses 3D position
local position = ray.Origin + ray.Direction * 10
-- use the position and direction to make a cframe and set the part.CFrame
part.CFrame = CFrame.LookAt(position, position + ray.Direction)
end)
I kinda understand it but i’m not a very good programmer
there are two ways i’d approach this.
- You could use a body mover that uses torque to rotate the ship to the mouses location
local gyro = instance.new("bodygyro",shipRootPart) --bodygyro is depricated so id suggest using another body mover
local mouse = player:GetMouse
--edit the gyro's settings to your liking
mouse.Move:Connect(function()
gyro.CFrame = mouse.Hit.p
end)
- I’d use a similar method to method 1, but i wouldnt use a gyro and just use
CFrame.new
orCFrame.lookAt
local mouse = player:GetMouse
mouse.Move:Connect(function()
shipRootPart.CFrame = CFrame.New(shipRootPart.CFrame, mouse.Hit.P)
--EDIT: Make sure to tween it or use runservice for the outcome to be smooth!
end)
keep in mind this is pseudo code so it wont work if you just copy and paste it, its just a basic idea of what you could do
i hope this helps