C# 玩家don';t移动缓慢(平稳)
我有一个问题,请在我的游戏中,当我在一个输入字段中写“左”并点击一个UI按钮时,立方体移动“左”并吃硬币(上、下、右都一样)。我的问题是当我在移动的玩家下方写下这段代码时,但不会慢慢消失,而不是出现在声明它的位置C# 玩家don';t移动缓慢(平稳),c#,unity3d,coroutine,C#,Unity3d,Coroutine,我有一个问题,请在我的游戏中,当我在一个输入字段中写“左”并点击一个UI按钮时,立方体移动“左”并吃硬币(上、下、右都一样)。我的问题是当我在移动的玩家下方写下这段代码时,但不会慢慢消失,而不是出现在声明它的位置 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class GameManager : MonoBehaviour
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public InputField mainInputField;
//public float speed;
public GameObject Player;
public Button Click_me;
public float smoothing = 1f;
public Transform TargetRight1;
public Transform TargetRight2;
public Transform TargetUP;
// Start is called before the first frame update
void Start()
{
}
public void SubmitName()
{
string[] lines = mainInputField.text.Split('\n');
for (int i = 0; i < lines.Length; i++)
{
if (lines[i] == "UP")
{
// moveUP();
StartCoroutine(MyCoroutineUP(TargetUP));
}
else if (lines[i] == "DOWN")
{
//MoveDown();
}
else if (lines[i] == "LEFT")
{
//MoveLeft();
}
else if (lines[i] == "RIGHT")
{
StartCoroutine(MyCoroutineUP(TargetRight1));
}
}
// Click_me.interactable = false;
}
IEnumerator MyCoroutineUP(Transform target)
{
while (Vector3.Distance(Player.transform.position, target.position) > 0.05f)
{
Player.transform.position = Vector3.Lerp(Player.transform.position, target.position, smoothing * Time.deltaTime);
}
yield return null;
}
}
玩家缓慢移动并获得硬币,但如果我有2个以上的直线,例如我写的左边,当我调用第一行中的函数时,while循环将无法正常工作。对不起,我的英语不好
您将获得并发的协同程序 听起来你真正想问的是如何堆叠多个命令,并一个接一个地执行它们。这变得有点复杂,但听起来像是一个完美的用例 现在在
平滑
中,您更希望设置对象移动1个单位所需的时间(以秒为单位)
当然,我不知道你对目标的确切设置,但这就是目标附在玩家身上时的样子(所以他们会跟着移动)
您的TargetRight、TargetUp到底是什么?它们是固定点还是一些硬币的位置?TaargetUP,TargetRight是一个空的游戏对象,在一个特定的位置,我希望玩家去它好的答案,你怎么能在stackoverflow中添加gif?@IgnacioAlorre和其他任何图像一样,只要它小于2Mb;)
while (Vector3.Distance(Player.transform.position, target.position) > 0.05f)
{
Player.transform.position = Vector3.Lerp(Player.transform.position, target.position, smoothing * Time.deltaTime);
yield return null;
}
private readonly Queue<Transform> _commands = new Queue<Transform>();
public void SubmitName()
{
var lines = mainInputField.text.Split('\n');
mainInputField.text = "";
foreach (var line in lines)
{
switch (line)
{
case "UP":
// adds a new item to the end of the Queue
_commands.Enqueue(TargetUp);
break;
case "DOWN":
_commands.Enqueue(TargetDown);
break;
case "LEFT":
_commands.Enqueue(TargetLeft);
break;
case "RIGHT":
_commands.Enqueue(TargetRight);
break;
}
}
StartCoroutine(WorkCommands());
}
private IEnumerator WorkCommands()
{
// block input
Click_me.interactable = false;
// run this routine until all commands are handled
while (_commands.Count > 0)
{
// returns the first element and at the same time removes it from the queue
var target = _commands.Dequeue();
// you can simply yield another IEnumerator
// this makes it execute and at the same time waits until it finishes
yield return MovementCoroutine(target);
}
// when done allow input again
Click_me.interactable = true;
}
private IEnumerator MovementCoroutine(Transform target)
{
var startPos = transform.position;
var targetPos = target.position;
var timePassed = 0f;
do
{
var lerpFactor = Mathf.SmoothStep(0, 1, timePassed / smoothing);
transform.position = Vector3.Lerp(startPos, targetPos, lerpFactor);
timePassed += Time.deltaTime;
yield return null;
}
while(timePassed < smoothing);
// just to be sure there is no over or undershooting
// in the end set the correct target position
transform.position = targetPos;
}
var distance = Vector3.Distance(startPos, targetPos);
var duration = smoothing * distance;
do
{
var lerpFactor = Mathf.SmoothStep(0, 1, timePassed / duration);
...
}
while (timePassed < duration);