PC-20230316NUNE\Administrator 2b467e56ad no message
2024-02-20 18:39:12 +08:00

39 lines
970 B
C#

using UnityEngine;
namespace HPJ.Presentation
{
public class MoveTowardsPoint : MonoBehaviour
{
public Transform target;
public float speed = 5f;
public float acceleration = 2f;
private void Start()
{
transform.SetParent(null);
}
private void Update()
{
// Get the direction to the target
Vector3 direction = target.position - transform.position;
// Calculate the distance to the target
float distance = direction.magnitude;
// Move towards the target
if (distance > 0.1f)
{
// Calculate the current speed
float currentSpeed = speed + acceleration * distance;
// Normalize the direction vector
direction.Normalize();
transform.position += direction * currentSpeed * Time.deltaTime;
}
}
}
}