Question by
WhyldH · Jun 05, 2016 at 12:07 AM ·
camera-movementanchortroubleshooting
[2D]Hybrid cursor and mouse following camera trouble [Resolved]
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).
Comment
Answer by WhyldH · Jun 05, 2016 at 04:13 PM
Here is the corrected camera, using lerp function for smoothing, instead of the crapy log system. ^^"
using UnityEngine;
using System.Collections;
public class HybridFollowCursor2DScript : MonoBehaviour
{
public Transform target;
public float maxDistance;
public float minHeight;
public float Damping;
void Start ()
{
if (target == null)
return; .
Vector2 iniCamPos = new Vector2 (target.position.x, minHeight);
rigidbody2D.MovePosition(iniCamPos);
}
void LateUpdate ()
{
Vector2 cursorPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 trgPos = target.position;
float currentx = transform.position.x;
float currenty = transform.position.y;
float dx = cursorPos.x - trgPos.x;
float dy = cursorPos.y - trgPos.y;
float camdx = currentx - trgPos.x;
float camdy = currenty - trgPos.y;
float wantedx = trgPos.x + dx;
float wantedy = trgPos.y + dy;
float trux = Mathf.Lerp (currentx, wantedx, Damping * Time.deltaTime);
float truy = Mathf.Lerp (currenty, wantedy, Damping * Time.deltaTime);
float basicx = Mathf.Lerp (currentx, trgPos.x, (Damping*2) * Time.deltaTime);
float basicy = Mathf.Lerp (currenty, minHeight, (Damping*2) * Time.deltaTime);
float effCamdx = Mathf.Log (Mathf.Abs (camdy));
float effCamdy = Mathf.Log (Mathf.Abs (camdy));
float truMxDistX = trgPos.x + maxDistance - effCamdy;
float truMnDistX = trgPos.x - maxDistance + effCamdy;
float truMxDistY = trgPos.y + maxDistance - effCamdy;
float truMnDistY = trgPos.y - maxDistance + effCamdy;
if (trux >= truMxDistX)
trux = truMxDistX;
else if (trux < truMnDistX)
trux = truMnDistX;
if (truy >= truMxDistY)
truy = truMxDistY;
else if (truy < truMnDistY)
truy = truMnDistY;
if (truy < minHeight)
truy = minHeight;
if (dx > maxDistance / 1.2 || dy > maxDistance / 1.2 || dx < -(maxDistance / 1.2) || dy < - (maxDistance / 1.2))
rigidbody2D.position = new Vector2 (trux, truy);
else
rigidbody2D.position = new Vector2 (basicx, basicy);
}
}
I've also added some little tweaks, to improve gameplay comfort. Maybe it'll inspire someone. ^^
Your answer