Hey guys. So I want to make a fighting game. And I need to do a “lock on” player. I can imagine how this will work, but I don’t have an idea how to script this. Could you please tell me how to script it?
Are you looking for a projectile to lock on to a player? If so you can use rocket propulsion to achieve your goal. I suggest taking a look here for more information.
u mean lock on humanoid?
Like on head or smthing?
I want lock it on to player humanoid
No, i want a camera be locked on player humanoid.
give me a sec, i will make it for u !
Thank you. But I wanted that someone explained to me how to make it.
The easiest way is to set the CurrentCamera’s CFrame to look towards the head/torso of the locked player. It works well. If you want something more advanced you will have to wait for other answers.
local CurrentCamera = workspace.CurrentCamera
CurrentCamera.CFrame = CFrame.new(CurrentCamera.Position, LockedPlayer.PrimaryPart.Position)
(Remember to wrap this in a RenderStepped loop so the camera keeps looking at the locked player);
To find a target, you can detect when a player presses a key with UserInputService (usually most people, including me, use the mouse wheel which is UserInputType.MouseButton3
), then use Mouse.Target, which returns the basepart the mouse is looking at. If there’s no basepart, it just returns nil. You can check if that part is parented to a model containing a humanoid and a humanoidrootpart, and if it founds them, then that’s a character. Remember to check that the model isn’t equal to the player’s character tho, you don’t want the player to be able to lock on himself.
Then, you can use a BillboardGui to give the locked player a gui which shows his position. It’s optional, but it looks better in my opinion.
This is the method I usually use, and it’s probably the easiest as said before.
Thank you. I gonna try to write a code with this.
Let me know if you do not understand how to do something and I’ll try to help you with it.
there is a better way of doing this by using the new Cframe.LookAt()
True true, I used CFrame.new()
cause I never used CFrame.LookAt()
so I didn’t wanna risk it. Maybe you can explain it to the OP too.
what do you mean by op please explain
OP is short for original poster.
local Cam = workspace.CurrentCamera
CurrentCamera.CFrame = CFrame.lookAt(Cam.Position, randomPlayer.HumanoidRootPart.Position)
i edited AndroidBoyz script,
this is the improved version