简体   繁体   English

我应该在哪里再次将标志设置为 false?

[英]Where should I set the flag to false again?

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

public class Follow : MonoBehaviour
{
    public Transform targetToFollow;
    public Text text;
    public float lookAtRotationSpeed;
    public float moveSpeed;

    private float minMoveSpeed = 0f;
    private bool moveInDistance = false;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        Vector3 lTargetDir = targetToFollow.position - transform.position;
        lTargetDir.y = 0.0f;
        transform.rotation = Quaternion.RotateTowards(transform.rotation,
            Quaternion.LookRotation(lTargetDir), Time.time * lookAtRotationSpeed);

        var distance = Vector3.Distance(transform.position, targetToFollow.position);

        text.text = distance.ToString();

        if(distance > 5f && moveInDistance == false)
        {
            moveSpeed = moveSpeed + 0.5f * Time.deltaTime;
        }

        if (distance > 1.5f && distance < 5f)
        {
            moveInDistance = true;
            moveSpeed = moveSpeed + 0.05f;
        }
        else if (distance < 1f)
        {
            moveSpeed = Mathf.Max(minMoveSpeed, moveSpeed - 0.3f);
        }

        transform.position = Vector3.MoveTowards(transform.position, targetToFollow.position, Time.deltaTime * moveSpeed);
    }
}

The first time the game is running the transform will move faster to the target in this part:游戏第一次运行时,变换将在这部分更快地移动到目标:

if(distance > 5f && moveInDistance == false)
            {
                moveSpeed = moveSpeed + 0.5f * Time.deltaTime;
            }

Then when the transform has reach close distance to the target he will keep following the target in slower speed in this part:然后当变换距离目标很近时,他会在这部分继续以较慢的速度跟随目标:

if (distance > 1.5f && distance < 5f)
            {
                moveInDistance = true;
                moveSpeed = moveSpeed + 0.05f;
            }

The problem is if in the future in the game I will want to change the speed again if the transform will be more then 5 distance from the target.问题是如果将来在游戏中我想再次改变速度,如果变换距离目标超过 5 倍。 Then where should I change the flag moveInDistance to false again?那么我应该在哪里再次将标志 moveInDistance 更改为 false 呢?

You could edit your existing if statement alittle to include what you are after:您可以稍微编辑现有的if statement以包含您所追求的内容:

if(distance > 5f)
{
  if (moveInDistance == false) {
    moveSpeed = moveSpeed + 0.5f * Time.deltaTime;
  } else {
    moveInDistance = false;
  }
}

You can put moveInDistance before moving logical like您可以在移动逻辑之前放置 moveInDistance

moveInDistance = distance > 1.5f && distance < 5f; // or other condition

if(distance > 5f && !moveInDistance)
{
    moveSpeed = moveSpeed + 0.5f * Time.deltaTime;
}
if (distance > 1.5f && distance < 5f)
{
    moveSpeed = moveSpeed + 0.05f;
}
else if (distance < 1f)
{
    moveSpeed = Mathf.Max(minMoveSpeed, moveSpeed - 0.3f);
}
...

Add example if else for comment添加示例 if else 进行评论

...

float ms = moveSpeed;

if(distance > 5f)
{
    ms = moveSpeed + 0.5f; //move faster
}
else if (distance < 1f)
{
    ms = Mathf.Max(minMoveSpeed, moveSpeed - 0.3f); // move slower
}else {
    //set your ms (movement speed) when follwer is move in distance range if you don’t need default value that equal moveSpeed here
}

// by default if distance in range of 1-5 ms is equal moveSpeed and IsInRange Case

// move object by using ms value
...

Ps.附言。 In my opinion I think moveInDistance is not important thing to use in my if-else condition you can check distance range in elseIf statement instead of set to moveInDistance在我看来,我认为 moveInDistance 在我的 if-else 条件中使用并不重要,您可以在 elseIf 语句中检查距离范围,而不是设置为 moveInDistance

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

相关问题 我应该在哪里将 animation 标志 bool 设置回 false? - Where should I set the animation flag bool back to false? 在哪里将ImplicitlyExpandDesignTimeFacades设置为false? - Where do I set ImplicitlyExpandDesignTimeFacades to false? 我应该如何返回以前设置的列表而不再次设置? - How should I return a list that was previously set without setting it again? 我应该如何以及在代码中的何处使用 loop bool 标志来决定是循环还是在航路点之间进行一次移动? - How and where in the code should I use the loop bool flag to decide if to loop or to make once movement between the waypoints? 即使存在XML元素,布尔标志也会设置为false - bool flag set to false even though XML Element Exists 我应该在哪里设置语言(CurrentThread.CurrentCulture)? - Where should I set the language (CurrentThread.CurrentCulture)? 我应该在Windows服务器的哪里设置TLS安全协议? - Where should I set the TLS Security Protocol in a windows server? 我该如何表示分层标志枚举? - How should I represent hierarchical flag enums? “for(bool flag = true; flag; flag = false){...}” - 这是正常的吗? - “for (bool flag = true; flag; flag = false) { … }” - is this normal? 我应该为asp.net mvc应用程序中常用引用的程序集设置copy local false吗? - Should I set copy local false for commonly referenced assemblies in asp.net mvc applications?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM