Having some issues with camera position
I currently have a script that takes the player's position, the mouse position, finds the mid-point and then places the camera at that point, so the player can see the area of play a little better.
However, the issue with this is that the player can then walk away from the mouse position and effectively walk off the edge of the screen.
What I'm trying to do to fix this is check the distance between the mouse position and the player position and check if it's within a certain limit. If it is, set the camera to the mid-point, if it isn't, then set the camera to be at the max distance I allow from the player but in the same direction as the mouse.
My issue seems to be more mathematics related than programming related, but I seem to have hit a road block I can't get around. I've spent a few hours mulling over the issue now and decided to finally turn to the community for help. Any ideas?
The code I'm using to place the camera at the mid-point is as follows:
using UnityEngine;
using System.Collections;
public class CameraFollow : MonoBehaviour
{
public Transform target;
public float smoothing = 5f;
public float cameraLimit;
Vector3 offset;
Vector3 mouseOffset;
void Start ()
{
offset = transform.position - target.position;
}
void FixedUpdate ()
{
mouseOffset = (target.position + (Camera.main.ScreenToWorldPoint (Input.mousePosition)) / 2) / 2;
Vector3 targetCamPos = mouseOffset + offset;
transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);
}
}
Answer by Whompratt · Jan 24, 2016 at 09:20 PM
So, after 5 days with no response I simultaneously realised my mistake and lost hope in getting community help.
It was something as simple as dividing the camera position by 2 without realising the effects this would have on it's movement.
What was this:
mouseOffset = (target.position + (Camera.main.ScreenToWorldPoint (Input.mousePosition)) / 2) / 2;
Became this:
mouseOffset = (target.position + Camera.main.ScreenToWorldPoint (Input.mousePosition));
and it solved my problem.
Thanks anyway.