Here’s the problemator :
using UnityEngine;
using System.Collections;
public class HybridFollowCursor2DScript : MonoBehaviour
{
public Transform target; // Target on wich the camera will be anchored.
public float maxDistance; // Longest distance the camera can get from the target.
public float minHeight; // Lowest vertical level the camera can get.
void Start ()
{
if (target == null)
return;
Vector2 iniCamPos = new Vector2 (target.position.x, minHeight); // Creat the "Vector2" Type needed for ".MovePositon(Vector2)" function.
rigidbody2D.MovePosition(iniCamPos);
}
void FixedUpdate ()
{
Vector2 cursorPos = Input.mousePosition; // cursor position
Vector2 trgPos = target.position; // target position
// Distances between the cursor and the target.
float dx = cursorPos.x - trgPos.x; // vertical distance
float dy = cursorPos.y - trgPos.y; // horizontal distance
// Use of another variable because we'll use logarithm right after that, and ".log" function can't handle negative numbers.
float adx; // the horizontal distance we'll had between the camera and the target
float ady; // the vertical distance we'll had between the camera and the target
if (dx < 0)
adx = - Mathf.Log (Mathf.Abs(dx)); // We're negating the value when the cursor is left-side of the target...
else
adx = Mathf.Log (dx);
if (dy < 0)
ady = - Mathf.Log (Mathf.Abs(dy));
else
ady = Mathf.Log (dy);
float trueAdy = trgPos.y + ady; // Here we need to combine the calcul for the vertical height within one variable because the limitation for the lowest point doesn'twork the same as the other limitations.
float trueAdx = trgPos.x + adx; // Not necessary at all, but maybe it'll do something positive ???
if (adx > maxDistance)
adx = maxDistance;
else if (adx < - maxDistance)
adx = - maxDistance;
if (ady > maxDistance)
ady = maxDistance;
if(trueAdy < minHeight)
trueAdy = minHeight; // "minHeight" needs to block the camera on a certain camera height (void is only good for coders).
// The True Line of Destiny ! ...
rigidbody2D.position = new Vector2 (trueAdx, trueAdy); //... except it doesn't work... and I don't know why. T_T
}
}
The problem is that the camera’s anchor isn’t centered on the target but way on the right…
I’ve searched for more than 2 or 3 hours now and still don’t know what’s going on.
So please if someone have the answer, let me know (even more if you share tips to optimise this monstruosity).