mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-06-27 20:04:35 +00:00
39 lines
970 B
C#
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;
|
|
}
|
|
}
|
|
}
|
|
}
|