[英]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.