- Home /
TopDown shooter camera
Need a Camera to look at the player and move the camera to the mouse cursor while keeping the player in view I don't really understand how I would do this Thanks
Could you clarify your idea a bit more ? I'm not really getting a clear picture around this idea. If you give some more information in your question I'm sure we can find something that suits your needs.
Like the camera moves to the cursor but keeps the character still in view of the camera for example, if I move the cursor in front of the camera it moves slightly forwards
Shows in games like Hotline $$anonymous$$iami or enter the gungeon
Answer by Zarenityx · Jun 18, 2019 at 09:36 PM
There's a lot of ways one could do this, but here's one:
Find the XZ world position of the cursor. You could use Camera.ScreenToWorldPoint for this, or, if you're already doing a raycast for selections or the like, you could use that. You just need the XZ position, since it's a top-down 2D game.
Get the difference between this position and the player's position. Along the lines of
diff = cursorPos - player.transform.position;
wherediff
andcursorPos
are of typeVector3
, andplayer
is your player reference (GameObject of Script).Declare a
public float maxDist;
that will represent how far away the camera is allowed to drift from the player. This value will depend on the size of your camera, environment, and how much of an effect you want the mouse to have overall. Optionally declaring a 'public float offsetAmt;' can let you minimize or exaggerate the offset effect as desired.Apply clamping to the magnitude, and optionally multiply by the
offsetAmt
if you're using it. Like so:diff = Mathf.Clamp(diff.magnitude*offsetAmt,0,maxDist)*diff.normalized;
(Optional) If you want it to be a smoother transition, you might want to declare a
public float lookSmoothSpeed;
and aVector3 oldDiff;
somewhere and include the linediff = Vector3.Lerp(oldDiff,diff,Time.deltaTime*lookSmoothSpeed); oldDiff = diff;' This will make the offset of the camera smoothly approach the correct value, slowing down as it gets closer, rather than just snapping to it, and can feel better. Experiment with values for
lookSmoothSpeed`, although I recommend starting off around 10. Note that when you start the script, it's probably a good idea to setoldDiff = player.transform.position;
.Apply the offset to the camera. Along the lines of
Vector3 campos = player.transform.position+diff; campos.y = camera.transform.position.y; camera.transform.position = campos;
The result should be a camera that moves a bit like how you want, although I should state as a disclaimer that nothing I just wrote is tested, and I wrote this all off the top of my head. In any case, this should get you headed in the right direction.
This is a 3d game, not a 2d game does the same concept apply?
Ah. Sorry you mentioned Hotline $$anonymous$$iami and Gungeon and I assumed 2D. As long as camera movement is all top-down, i.e. constrained to the XZ plane, then yes this definitely works. If you're doing a perspective camera and moving it up and down to look closer or farther then you'd need to scale the maxDist and offsetAmt accordingly. For cameras that are 'mostly' top-down, but rotate a bit, this technique may or may not be valid, depending on the desired game feel and the amount of rotation.
If the camera movement can be considered 2D for all intents and purposes, even if rendering is 3D, then yes, there is value in this technique.
Your answer
Follow this Question
Related Questions
Proper/Standard way to create a top down / isometric sprite sorting solution 1 Answer
I am writing a camera script, but it seems not to be working. Any suggestions? 1 Answer
2D Top Down Camera Smoothing 1 Answer
How to Move Top Down Camera Towards Game Object on Touch? 1 Answer
Deadfrontier type crosshair 1 Answer