- Home /
Terraria-esque 'scope' function? (2D)
Hey,
I've been developing this game of mine for a couple of months now and I've been managing surprisingly well, many thanks to Unity Answers, however, I've come across something I've been unable to solve. Much like the function of the sniper scope/rifle in Terraria I'd like to have the camera focus on a point about half-way between the cursor and the player. So far I've just managed to somehow make the player fly very fast in the Z axis.
For those who don't know what the scope is like:
When your cursor is on the player, the camera is centered, however, as you move your cursor outward the camera smoothly moves to roughly half way between the cursor and the player though the player is always in sight. Link because I doubt I've explained it well: Link!
Explanation end.
I would provide my code, however, I fear it is horrendously wrong, considering the player catapulting (I have no idea why it's happening.) It is probably best to start afresh with it, for me. Any insight on how to achieve such a thing would be excellent. Also, could it be in C# if possible. Though I'd be able to convert from JS, it'd be a little easier.
Thank you.
Answer by KellyThomas · Jan 23, 2014 at 11:46 PM
Well the focus point is just the mean of the player and mouse positions:
public Transform player;
public Transform camera;
pubic float cameraLag = 0.5f;
private float cameraZPosition;
void Start() {
cameraZPosition = camera.position.z;
}
void Update() {
//find ideal point for cameraFocus
Vector3 mousePosition = camera.ScreenToWorldPoint(Input.mousePosition);
Vector3 cameraFocus = (player.position + mousePosition) / 2;
cameraFocus.z = cameraZPosition;
//move camera closer to cameraFocus
Vector3 offset = cameraFocus - camera.position;
float ratioToMove = Mathf.Clamp(Time.deltaTime / cameraLag, 0.0f, 1.0f);
camera.position += offset * ratioToMove;
}
Ah, thanks. I attempted this before but I was obviously trying to over-complicate it. This is good thanks, however, the camera is moving in on the Z axis toward 0. Any idea why this is?
Um... I 'm coding in browser not IDE and wont be home until this evening so there could be some kind of error i'm not spotting.
Try Debug.Log(cameraFocus)
to see this that value is looking correct... that should help isolate any issues.
Thanks for the effort at least! The output of cameraFocus keeps changing. It starts at the original location then the cameraFocus.z tends to 0. I /nearly/ fixed it by adding to the z of cameraFocus, however, I end up with a jittering camera.
Then we need to look into the value of mousePosition
, if that is off then you will likely need to adjust the z component of the vector passed to `ScreenToWorldPoint()`.
Great, that has worked excellently! Though the focus is just on the player. The mouse doesnt seem to have factored into it. I tried Debug.Log-ing it, however, it returned values don't seem to correspond to the mouse's position. I can't figure out what it is supposed to be tracking. In the morning I'm going to try making a GameObject follow the mouse and use it's position ins$$anonymous$$d of the mouse. Thanks for all your help, and for putting me on track at least.
Your answer
![](https://koobas.hobune.stream/wayback/20220613131246im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to update ScreenToWorldPoint before Camera renders? 1 Answer
All Sprites Instantiate In top right of screen for some reason. 0 Answers
How to make camera position relative to a specific target. 1 Answer
Move character in direction its facing 1 Answer
Camera relative rigidbody third person player movement 1 Answer