简体   繁体   English

Unity3D - 为什么我的对象不会每秒移动每一帧

[英]Unity3D - Why my object doesnt move every frame per second

Im struggling with very weird and propably simple problem,I have created a robot and his task is to move towards target I mean Im using Vector3.MoveTowards function in void update to move my robot per every frame but the problem is that he is moving only one time and stopping, he is making one step instead of for example 100. Im working with Unity3D.我正在努力解决非常奇怪且可能很简单的问题,我创建了一个机器人,他的任务是向目标移动我的意思是我在无效更新中使用 Vector3.MoveTowards 函数来每帧移动我的机器人,但问题是他只在移动一次并停下来,他正在迈出一步,而不是例如 100。我正在使用 Unity3D。 Here is source code;这是源代码;

public class Test01 : MonoBehaviour
{
public float speed, stopDist, rotationSpeed, moveSpeed, minSpeed, maxBackSpeed, maxFrontSpeed, turnSpeed, riseSpeed;
public Transform target;
private Rigidbody rb;
private float currentSpeed;

public bool isFinding = false;
private PlaySound signal;

void Start()
{
    signal = GameObject.FindGameObjectWithTag("Signal").GetComponent<PlaySound>();
    rb = GetComponent<Rigidbody>();
}

private void Update()
{
    // If space key button is pressed the robot's isFinding bool is becaming true and robot is starting searching for target.
    if (Input.GetKeyDown(KeyCode.Space))
    {
        isFinding = true;
        if (Vector3.Distance(transform.position, target.position) > stopDist)
        {
            transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
            signal.isAlarming = true;
            signal.Sound();
        }
        else if (Vector3.Distance(transform.position, target.position) < stopDist)
        {
            isFinding = false;
            signal.isAlarming = false;
            SoundManager.instance.StopSound();
            StopChasing();
        }
    }
}
 public void StopChasing()
{
    transform.position = this.transform.position;
}

Input.GetKeyDown is a function right when you pressed down the key. Input.GetKeyDown是您按下键时的功能。

You might want to change that to Input.GetKey instead, so it will move everytime you hold space.您可能希望将其更改为Input.GetKey ,这样每次您保持空间时它都会移动。

Like this,像这样,

private void Update()
{
    // If space key button is pressed the robot's isFinding bool is becaming true and robot is starting searching for target.
    if (Input.GetKey(KeyCode.Space))
    {
        isFinding = true;
        if (Vector3.Distance(transform.position, target.position) > stopDist)
        {
            transform.position = Vector3.MoveTowards(transform.position, target.position, speed* Time.deltaTime);
            signal.isAlarming = true;
            signal.Sound();
        }
        else if (Vector3.Distance(transform.position, target.position) < stopDist)
        {
            isFinding = false;
            signal.isAlarming = false;
            SoundManager.instance.StopSound();
            StopChasing();
        }
    }
}

If you take the Input.GetKeyDown for the robot to start targetting.如果您使用Input.GetKeyDown为机器人开始定位。 Then split it like this然后像这样分割

private void Update()
{
    // If space key button is pressed the robot's isFinding bool is becaming true and robot is starting searching for target.
    if (Input.GetKeyDown(KeyCode.Space))
    {
        isFinding = true;
    }
    if (isFinding)
    {
        if (Vector3.Distance(transform.position, target.position) > stopDist)
        {
            transform.position = Vector3.MoveTowards(transform.position, target.position, speed* Time.deltaTime);
            signal.isAlarming = true;
            signal.Sound();
        }
        else if (Vector3.Distance(transform.position, target.position) < stopDist)
        {
            isFinding = false;
            signal.isAlarming = false;
            SoundManager.instance.StopSound();
            StopChasing();
        }
    }
}

After all, what you're doing before doesn't really use the isFinding as anything, you'll be just setting it as true and it will do nothing to affect the code.毕竟,您之前所做的并没有真正使用isFinding作为任何东西,您只需将其设置为 true 并且它不会对代码产生任何影响。

Well currently you have everything nested under the GetKeyDown so it is executed only exactly once .好吧,目前您将所有内容都嵌套在GetKeyDown下,因此它只执行一次

You could change that doing你可以改变那样做

if (Input.GetKeyDown(KeyCode.Space))
{
    isFinding = true;
}

if(isFinding)
{
    if (Vector3.Distance(transform.position, target.position) > stopDist)
    {
        transform.position = Vector3.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
        signal.isAlarming = true;
        signal.Sound();
    }
    else
    {
        isFinding = false;
        signal.isAlarming = false;
        SoundManager.instance.StopSound();
        StopChasing();
    }
}

So pressing Space once activates the isFinding mode until it arrives at Vector3.Distance(transform.position, target.position) < stopDist所以按一次Space激活isFinding模式,直到它到达Vector3.Distance(transform.position, target.position) < stopDist

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM