【发布时间】:2021-12-31 06:46:23
【问题描述】:
我正在编写将对象移动到随机位置的代码。 我做了一个函数来决定随机坐标并返回它。 但是,我认为功能并没有连接在一起。 这是我尝试过的......
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public float speed;
Vector3 target;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 target = random(target);
transform.position = Vector3.MoveTowards(transform.position, target, Time.deltaTime * speed);
}
Vector3 random(Vector3 target)
{
float min = -100.0f;
float max = 100.0f;
float randomX = Random.Range(min, max);
float randomZ = Random.Range(min, max);
Vector3 target = new Vector3(randomX, 10.0f, randomZ);
return target;
}
}
这是我收到的错误消息。
Assets\Movement.cs(31,17): error CS0136: A local or parameter named 'target' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
我该如何解决这个问题?
【问题讨论】:
-
请将代码发布为 文本 而不是图像。还要说明您所看到的与您期望看到的,以及您为诊断问题所做的工作(例如,如果您在调试器中运行代码会发生什么)。
-
您遇到的具体问题是什么?运行代码或错误的结果是什么?
-
老实说,你很可能不想在每一帧都定义一个新的移动方向......
标签: c# visual-studio function unity3d random