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

Unity中顾客从随机坐标点移动至定点的代码问题求助

Unity顾客移动问题排查与修复方案

问题根源分析

  • 代码中起始坐标为固定值,未实现需求的随机范围生成
  • 直接修改transform.position会和Rigidbody的物理模拟冲突,导致到达目标后下落
  • 原Lerp循环存在精度问题,可能无法准确停在目标点

修复步骤与代码示例

1. 实现随机起始坐标生成

将固定起始点替换为X轴-7到5之间的随机坐标,保持需求的Y、Z轴数值:

private Vector3 GetRandomStartPos()
{
    float randomX = Random.Range(-7f, 5f);
    return new Vector3(randomX, 7.8f, -19f);
}

2. 修正移动逻辑,兼容Rigidbody

如果使用Rigidbody,改用Rigidbody.MovePosition保证物理同步;同时优化计时逻辑,确保到达目标后稳定停止:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CustomerMovement : MonoBehaviour
{
    private Vector3 endPos = new Vector3(-1f, 7.8f, -7.5f);
    public float moveDuration = 5f;
    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        // 初始冻结Y轴和旋转,避免生成时受重力影响偏移
        if(rb != null)
        {
            rb.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezeRotation;
        }
        Vector3 randomStartPos = GetRandomStartPos();
        StartCoroutine(MoveToTarget(randomStartPos, endPos, moveDuration));
    }

    Vector3 GetRandomStartPos()
    {
        float randomX = Random.Range(-7f, 5f);
        return new Vector3(randomX, 7.8f, -19f);
    }

    IEnumerator MoveToTarget(Vector3 startPos, Vector3 targetPos, float duration)
    {
        float elapsedTime = 0f;
        while(elapsedTime < duration)
        {
            elapsedTime += Time.deltaTime;
            float t = Mathf.Clamp01(elapsedTime / duration);
            Vector3 newPos = Vector3.Lerp(startPos, targetPos, t);
            
            if(rb != null)
            {
                rb.MovePosition(newPos);
            }
            else
            {
                transform.position = newPos;
            }

            yield return null;
        }

        // 强制修正最终位置,确保精准到达目标点
        if(rb != null)
        {
            rb.MovePosition(targetPos);
            // 到达后冻结所有位置,防止下落
            rb.constraints = RigidbodyConstraints.FreezeAll;
        }
        else
        {
            transform.position = targetPos;
        }
    }

    void Update()
    {
        
    }
}

3. 模型轴心检查

若修改代码后仍有空中漂浮问题,检查顾客模型的**轴心(Pivot)**位置:

  • 打开模型导入设置,将Mesh的Pivot设置为Bottom,让模型底部与坐标Y轴对齐
  • 或微调代码中的Y轴数值,匹配模型底部实际高度

额外说明

  • 不需要物理交互时,可直接移除Rigidbody组件,保留transform移动逻辑即可
  • Mathf.Clamp01用于确保插值进度始终在0-1区间,避免过度移动

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 22:07:31