Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

camera follow script car unity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour
{

	public Transform target; // child object of your car
	public float distance = 20.0f; // distance of camera from car
	public float height = 5.0f; // Camera height
	public float heightDamping = 2.0f; // smoothness
	public float xOffset = 0;
	public float lookAtHeight = 0.0f;
	public Rigidbody parentRigidbody; // Car
	public float rotationSnapTime = 0.3F;
	public float distanceSnapTime;
	public float distanceMultiplier;
	private Vector3 lookAtVector;
	private float usedDistance;
	float wantedRotationAngle;
	float wantedHeight;
	float currentRotationAngle;
	float currentHeight;
	Quaternion currentRotation;
	Vector3 wantedPosition;
	private float yVelocity = 0.0F;
	private float zVelocity = 0.0F;
    
	void Start()
	{
		lookAtVector = new Vector3(0, lookAtHeight, 0);
	}
    
	void LateUpdate()
	{
		wantedHeight = target.position.y + height;
		currentHeight = transform.position.y;
		wantedRotationAngle = target.eulerAngles.y;
		currentRotationAngle = transform.eulerAngles.y;
		currentRotationAngle = Mathf.SmoothDampAngle(currentRotationAngle, wantedRotationAngle, ref yVelocity, rotationSnapTime);
		currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
		wantedPosition = target.position;
		wantedPosition.y = currentHeight;
		wantedPosition.x = target.position.x + xOffset;
		usedDistance = Mathf.SmoothDampAngle(usedDistance, distance + (parentRigidbody.velocity.magnitude * distanceMultiplier), ref zVelocity, distanceSnapTime);
		wantedPosition += Quaternion.Euler(0, currentRotationAngle, 0) * new Vector3(0, 0, -usedDistance);
		transform.position = wantedPosition;
		transform.LookAt(target.position + lookAtVector);
	}

}
Comment

PREVIOUS NEXT
Code Example
Csharp :: check if internet is connected with c# winforms 
Csharp :: Configure Automapper 
Csharp :: find genre of song 
Csharp :: operator -- c# 
Csharp :: No migrations configuration type was found in the assembly 
Csharp :: c# get country code 
Csharp :: how to look around with mouse in unity 
Csharp :: c# get all enum values 
Csharp :: route attribute controller with parameter asp.net core 
Csharp :: Pass Querystring in C# httpclient 
Csharp :: validating file upload asp.net core mvc 
Csharp :: array join c# 
Csharp :: what is botnet attack 
Csharp :: linq get a dictionary key and value c# 
Csharp :: c# generate unique key 
Csharp :: datatable linq where clause c# 
Csharp :: public tmpro text 
Csharp :: show snackbar without scaffold flutter 
Csharp :: ffmpeg add audio to video at specific time 
Csharp :: if statement c# 
Csharp :: convert list string to list long c# 
Csharp :: minimize maximize restore wpf buttons 
Csharp :: how to print statement in c# 
Csharp :: convert decimal to 2 decimal places c# 
Csharp :: c# findindex 
Csharp :: click in vue 
Csharp :: addd to array c# 
Csharp :: how to add to a list only items that are not already in the list c# 
Csharp :: unity tilemap get all tiles 
Csharp :: what value of combobox index c# 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =