You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity跟随目标脚本问题:需维持最小1.5单位跟随距离

Fix for Follower Creeping Too Close to Stationary/Moving Target

Hey Daniel, let's sort out that frustrating issue where your follower keeps edging closer to the target until it touches it, even when the target only moves a tiny bit. The root problems in your original code are redundant movement calls, inconsistent distance-based logic, and a rotation speed bug that would have caused unintended behavior over time.

Here's the revised code that meets your exact requirements:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class Follow : MonoBehaviour {
    public Transform targetToFollow;
    public Text text;
    public Text text1;
    public float lookAtRotationSpeed;
    public float moveSpeed;
    private float minMoveSpeed = 0f;
    private Vector3 previousTargetPos;

    // Start is called before the first frame update
    void Start() {
        previousTargetPos = targetToFollow.position;
    }

    // Update is called once per frame
    void Update() {
        // Rotate towards the target (keep y-axis locked)
        Vector3 lTargetDir = targetToFollow.position - transform.position;
        lTargetDir.y = 0.0f;
        transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(lTargetDir), Time.deltaTime * lookAtRotationSpeed);

        // Calculate and display distance to target (formatted for readability)
        var distance = Vector3.Distance(transform.position, targetToFollow.position);
        text.text = "Transform Distance From Target " + distance.ToString("F2");

        // Adjust movement speed: boost when more than 5 units away
        float ms = moveSpeed;
        if (distance > 5f) {
            ms += 0.5f;
        }

        // Only move if we're outside the 1.5 unit minimum distance
        if (distance > 1.5f) {
            transform.position = Vector3.MoveTowards(transform.position, targetToFollow.position, Time.deltaTime * ms);
        }

        // Track target's previous position for potential future checks
        previousTargetPos = targetToFollow.position;
    }
}

Key Changes & Explanations

  • Strict Minimum Distance Enforcement: We only trigger movement when the follower is more than 1.5 units away from the target. Once it reaches that threshold, it stops moving entirely—no more slow, incremental adjustments that cause it to creep closer than intended.
  • Fixed Rotation Speed: Your original code used Time.time for rotation, which would have made the follower rotate faster and faster over time. We switched to Time.deltaTime to ensure rotation speed stays consistent, regardless of frame rate.
  • Removed Redundant Logic: The duplicate Vector3.MoveTowards call and confusing stationary position check have been removed. If the target moves and pulls the distance over 1.5 units, the follower immediately resumes following.
  • Improved Debug Readability: Added ToString("F2") to format the distance display to two decimal places, making it easier to monitor the follower's position relative to the target.

This implementation will keep your follower locked at a minimum of 1.5 units away, speed up to catch up when it's more than 5 units out, and only move when it needs to—no more unwanted creeping!

内容的提问来源于stack exchange,提问作者Daniel Lip

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:15:59