我是 C# 新手,在将 Unity 中的对象从随机点 A 移动到设定点 B 时遇到问题

问题描述 投票:0回答:1

我正在通过 Unity 学习如何在 Unity 中制作游戏,并且我终于开始开始使用 C#。不幸的是,我的代码遇到了问题。我正在制作一个游戏,让顾客走到收银台,我需要他们从一组随机坐标中生成并走到设定点。

我尝试过使用我过去编写的代码,同时也查看了一些不同的教程和技巧,所有这些都以相同的结果结束。我需要它们在 ((-7, 5), 7.8, -19) 之间生成并移动到 (-1, 7.8, -7.5)。无论我如何尝试,顾客都会在空中产卵并漂浮到他们需要前往的地点上方。它似乎将坐标数字加倍,但如果我将数字减半,它们会做完全相同的事情。我认为如果我添加一个刚体可能会有所帮助,但这只会导致客户在到达他们认为的 B 点时无休止地跌倒。有什么建议吗?

这是我当前正在使用的代码。

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

public class CustomerMovement : MonoBehaviour
{
    private Vector3 beginPos = new Vector3(-1f, 7.8f, -19f);
    private Vector3 endPos = new Vector3(-1f, 7.8f, -7.5f);
    public float time = 5;

    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(Move(beginPos, endPos, time));
    }

    IEnumerator Move(Vector3 beginPos, Vector3 endPos, float time)
    {
        for(float t = 0; t < 1; t += Time.deltaTime / time)
        {
            transform.position = Vector3.Lerp(beginPos, endPos, t);
            yield return null;
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
c# unity-game-engine
1个回答
0
投票

您当前的代码看起来不错,要进行随机生成,您需要的只是生成随机 x 和 z 值并在该位置创建一个对象的函数,之后您当前的移动脚本应该可以正常工作。似乎您已经尝试过创建随机生成的方法,如果您可以在此处显示代码,将会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.