Advertisement
AlphaPenguino

FollowPlayer.cs

Feb 11th, 2025
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class FollowPlayer : MonoBehaviour
  6. {
  7.    public float moveSmoothness;
  8.    public float rotSmoothness;
  9.  
  10.    public Vector3 moveOffset;
  11.    public Vector3 rotOffset;
  12.  
  13.    public Transform player;
  14.  
  15.     private void FixedUpdate()
  16.     {
  17.         FollowCar();
  18.        
  19.     }
  20.  
  21.     void FollowCar() {
  22.         HandleMovement();
  23.         HandleRotation();
  24.     }
  25.     void HandleMovement()
  26.     {
  27.         Vector3 targetPos = new Vector3();
  28.         targetPos = player.TransformPoint(moveOffset);
  29.         transform.position = Vector3.Lerp(transform.position, targetPos,moveSmoothness * Time.deltaTime);
  30.        
  31.  
  32.     }
  33.     void HandleRotation()
  34.     {
  35.         var direction = player.position - transform.position;
  36.         var rotation = new Quaternion();
  37.         rotation = Quaternion.LookRotation(direction + rotOffset, Vector3.up);
  38.         transform.rotation = Quaternion.Lerp(transform.rotation, rotation, rotSmoothness * Time.deltaTime);
  39.        
  40.     }
  41.  
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement