Racing_Game/Assets/CameraObjec.cs

28 lines
779 B
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraObjec : MonoBehaviour
{
public Transform player;
private Rigidbody playerRB;
public Vector3 Offset;
public float speed;
// Start is called before the first frame update
void Start()
{
playerRB = player.GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
Vector3 playerForward = (playerRB.velocity + player.transform.forward).normalized;
transform.position = Vector3.Lerp(transform.position,
player.position + player.transform.TransformVector(Offset)
+ playerForward * (-5f),
speed * Time.deltaTime);
transform.LookAt(player);
}
}