Hey, I’m currently using the smooth look camera script: http://wiki.unity3d.com/index.php/SmoothMouseLook
and I was wondering if there’s a way to make the camera drawn towards points of interest.
As in if you move the camera near a certain object with the right script it would have a magnetic feel to it.
So far I’ve only been able to manage a hacked together hard look that’s still kinda glitchy:Code
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CameraController : MonoBehaviour
{
#region Fields
// SETTINGS //
public float sensitivityX = 15f;
public float sensitivityY = 15f;
// VARIABLES //
private float rotationX = 0f;
private float rotationY = 0f;
private List<float> rotationArrayX = new List<float>();
private List<float> rotationArrayY = new List<float>();
private float frameCounter = 20f;
private Quaternion originalRotation;
private Vector3 currentFocusPoint = Vector3.zero;
[System.NonSerialized] public List<CameraFocusPoint> focusPoints = new List<CameraFocusPoint>();
#endregion
#region Unity Functions
void Start()
{
originalRotation = transform.localRotation;
}
void Update()
{
currentFocusPoint = Vector3.zero;
CalculateCameraFocusPoint();
CalculateLerp();
}
#endregion
#region Functions
public void CalculateCameraFocusPoint()
{
// If the player is free looking
if (Input.GetMouseButton(1))
{
// Check if there's any camera focus points in range.
// Start by finding the nearest focus point to the mouse.
float nearestDistance = float.MaxValue;
CameraFocusPoint nearestPoint = null;
foreach (CameraFocusPoint focusPoint in focusPoints)
{
Vector3 point = Camera.main.WorldToScreenPoint(focusPoint.transform.position);
float distance = Vector2.Distance(point, new Vector2(Screen.width / 2, Screen.height / 2));
if (point.z > 0 && distance < nearestDistance && distance < focusPoint.cameraFocusDistance)
{
nearestDistance = distance;
nearestPoint = focusPoint;
}
}
if (nearestPoint != null) // FOCUSING //
{
currentFocusPoint = nearestPoint.transform.position;
}
else // NORMAL FREE LOOK //
{
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
}
}
else // Otherwise keep the camera normal.
{
rotationX = 0;
rotationY = 0;
}
}
public void CalculateLerp()
{
// If we don't have a focus point
if (currentFocusPoint == Vector3.zero)
{
// Add it to the total.
rotationArrayX.Add(rotationX);
rotationArrayY.Add(rotationY);
// Limit the collection.
if (rotationArrayX.Count >= frameCounter)
{
rotationArrayX.RemoveAt(0);
}
if (rotationArrayY.Count >= frameCounter)
{
rotationArrayY.RemoveAt(0);
}
// Calculate the average rotation.
float rotationAverageX = 0f;
float rotationAverageY = 0f;
for (int i = 0; i < rotationArrayX.Count; i++)
{
rotationAverageX += rotationArrayX[i];
}
for (int i = 0; i < rotationArrayY.Count; i++)
{
rotationAverageY += rotationArrayY[i];
}
rotationAverageX /= rotationArrayX.Count;
rotationAverageY /= rotationArrayY.Count;
Quaternion xQuaternion = Quaternion.AngleAxis(rotationAverageX, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis(rotationAverageY, Vector3.left);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
else // If we do.
{
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(currentFocusPoint - transform.position), Time.deltaTime * 1);
}
}
#endregion
}
Thanks in advance.