xirang/Assets/scripts/camfollow.cs

33 lines
1.0 KiB
C#
Raw Permalink Normal View History

2024-11-26 22:01:14 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class camfollow : MonoBehaviour
{
public Transform target; // Ҫ<><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EAA3AC><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҽ<EFBFBD>ɫ
2024-11-27 00:06:25 +08:00
public float smoothSpeed = 0.001f; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD>ٶ<EFBFBD>
2024-11-26 22:01:14 +08:00
public Vector3 offset; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD>ƫ<EFBFBD><C6AB><EFBFBD><EFBFBD>
2024-11-27 00:06:25 +08:00
public float rotationSmoothSpeed = 50f; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>ƽ<EFBFBD><C6BD><EFBFBD>ٶ<EFBFBD>
public float maxDistance = 10f; // <20><><EFBFBD><EFBFBD>׷<EFBFBD>پ<EFBFBD><D9BE><EFBFBD>s
public float minDistance = 5f; // <20><>С׷<D0A1>پ<EFBFBD><D9BE><EFBFBD>
2024-11-26 22:01:14 +08:00
void LateUpdate()
{
if (target == null)
{
return;
}
// <20><><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF>λ<EFBFBD><CEBB>
Vector3 desiredPosition = target.position + offset;
2024-11-27 00:06:25 +08:00
transform.position = desiredPosition;
2024-11-26 22:01:14 +08:00
2024-11-27 00:06:25 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ŀ<EFBFBD><EFBFBD><EAB7BD>
Vector3 direction = target.position - transform.position;
Quaternion targetRotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * rotationSmoothSpeed);
2024-11-26 22:01:14 +08:00
}
}