- Home /
Question by
jcordeiro · Sep 17, 2010 at 05:53 PM ·
camera-movementcar-tutorial
How do I make the view camera look ahead of the car in the car tutorial?
How do I make the camera look ahead when conering, I want to make the camera move back from the view slightly on acceleration and move in closer to the car on braking. I would also like to know how to rotate the camera left or right at the apex of a hairpin or any corner.
Comment
Answer by mak · May 24, 2012 at 07:38 AM
I can only hazard a guess ,I think you would have to expand the smoothfollow.js ,sorry thats all i got ,im a terrible coder ,but I can create anything in a 3D package :p
Answer by hnm938 · Oct 06, 2018 at 10:59 PM
lol a little too late
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
public class CameraController : MonoBehaviour {
public bool lookSmoothDamp;
public bool lookAhead;
public float lookAheadAmmount = 2;
public float smoothDamp = .3f;
public GameObject playerTarget;
[Range(2, 10)]
private float cameraFOV;
Camera cam;
Vector3 velocity = Vector3.zero;
void Start ()
{
cameraFOV = 5;
cam = GetComponent<Camera>();
}
void FixedUpdate ()
{
var playerVelocity = playerTarget.GetComponent<Rigidbody2D>().velocity;
Vector3 playerVelocityVector = new Vector3(playerVelocity.x, playerVelocity.y, -10f);
Vector3 targetPlayerVector = new Vector3(playerTarget.transform.position.x, playerTarget.transform.position.y, -10f);
transform.position = Vector3.SmoothDamp(transform.position, targetPlayerVector + playerVelocityVector, ref velocity, smoothDamp);
cam.orthographicSize = cameraFOV;
if (lookSmoothDamp == false)
{
transform.position = targetPlayerVector;
}
if (lookSmoothDamp == true)
{
transform.position = Vector3.SmoothDamp(transform.position, targetPlayerVector, ref velocity, smoothDamp);
}
if (lookAhead == true)
{
transform.position = Vector3.SmoothDamp(transform.position, targetPlayerVector + playerVelocityVector * lookAheadAmmount, ref velocity, smoothDamp);
}
if (Input.GetKeyDown(KeyCode.RightBracket) && cameraFOV < 10)
{
cameraFOV += 1;
}
if (Input.GetKeyDown(KeyCode.LeftBracket) && cameraFOV > 2)
{
cameraFOV -= 1;
}
if (Input.GetKey(KeyCode.LeftBracket) && Input.GetKey(KeyCode.RightBracket))
{
cameraFOV = 5;
}
}
}