Anyone know how I would modify this script to have it be closer to my player?
Everything else is fine and it rotates when I turn then the camera will flip to the other side of my player but I cant seem to get the distance to the player correct. I’ve played with the field of view 15 but that only does so much.
using UnityEngine;
using System.Collections;
public class CarCamera : MonoBehaviour
{
public Transform target = null;
public float height = 0.5f;
public float positionDamping = 1f;
public float velocityDamping = 1f;
public float distance = 0.5f;
public LayerMask ignoreLayers = -1; //everything
public float Xx = 15f;
public float Yy = 20f;
public float Xe = 7f;
public float Ye = 2f;
private RaycastHit hit = new RaycastHit();
private Vector3 prevVelocity = Vector3.zero;
private LayerMask raycastLayers = -1;
private Vector3 currentVelocity = Vector3.zero;
void Start()
{
raycastLayers = ~ignoreLayers;
}
void FixedUpdate()
{
currentVelocity = Vector3.Lerp(prevVelocity, target.root.rigidbody.velocity, velocityDamping * Time.deltaTime);
currentVelocity.y = 0;
prevVelocity = currentVelocity;
}
void LateUpdate()
{
float speedFactor = Mathf.Clamp01(target.root.rigidbody.velocity.magnitude / 70.0f);
camera.fieldOfView = Mathf.Lerp(Xx, Yy, speedFactor);
float currentDistance = Mathf.Lerp(Xe, Ye, speedFactor);
currentVelocity = currentVelocity.normalized;
Vector3 newTargetPosition = target.position + Vector3.up * height;
Vector3 newPosition = newTargetPosition - (currentVelocity * currentDistance);
newPosition.y = newTargetPosition.y;
Vector3 targetDirection = newPosition - newTargetPosition;
if(Physics.Raycast(newTargetPosition, targetDirection, out hit, currentDistance, raycastLayers))
newPosition = hit.point;
transform.position = newPosition;
transform.LookAt(newTargetPosition);
}
}
To understand it more you can attach it to a camera then target a player fps walker or something its tough to explain