[英]2d sprite goes too fast with transform.translate in C# script (unity)
Sup. 燮。 My sprite goes too fast when I press the C button.
当我按下C按钮时,我的子画面过快。 It's a wolf that will lunge as an attack.
这是一头狼,会作为攻击而冲刺。 But it just goes from one spot to the next, and I got the idea that I'm simply using the wrong kind of code entirely.
但这只是从一个地方到另一个地方,我想到了我只是完全使用了错误类型的代码。 I'm guessing it has to do more with
Rigidbody2D = new Vector2
.... but I don't know where to go from there. 我猜想它与
Rigidbody2D = new Vector2
....的关系更多,但我不知道从那里去哪里。 Here's what I'm working with currently. 这是我目前正在使用的工具。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class tulMoveMount : MonoBehaviour {
private Animator anim;
private Rigidbody2D rb;
private bool goRight = true;
private bool jump = false;
private bool idle = true;
public float lungeDistance;
public float lungeSpeed;
public float lungeHeight;
void Start ()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
}
void Update ()
{
HandleCommands ();
}
void HandleCommands()
{
if (!jump && goRight && Input.GetKeyDown (KeyCode.C)) {
idle = false;
jump = true;
anim.SetTrigger ("jump");
rb = transform.Translate (lungeSpeed + lungeDistance, lungeHeight, 0); // HERE
idle = true;
jump = false;
anim.SetTrigger ("idle");
}
if (!jump && !goRight && Input.GetKeyDown (KeyCode.C)) {
idle = false;
jump = true;
anim.SetTrigger ("jump");
rb = transform.Translate (lungeSpeed + -lungeDistance, lungeHeight, 0); // HERE
idle = true;
jump = false;
anim.SetTrigger ("idle");
}
}
}
将平移乘以Time.DeltaTime可以在许多帧上平滑移动,然后您只需要微调lungeSpeed
速度即可获得所需的速度。
rb = transform.Translate ((lungeSpeed + lungeDistance, lungeHeight, 0)*Time.deltaTime);
To get a smooth translation from one point to another you can use Lerp inside a Corouting . 要获得从一个点到另一个点的平滑转换,可以在Corouting内部使用Lerp 。
In Lerp the first parameter is the start position and the second the targe. 在Lerp中,第一个参数是起始位置,第二个是目标。 The third parameter is a float between 0 and 1. If it's 0, you get the first parameter in Lerp.
第三个参数是0到1之间的浮点数。如果为0,则表示Lerp中的第一个参数。 If it's 1, you get the second.
如果为1,则获得第二个。 If 0.5 a middle point between both and so on...
如果两者之间的中间值为0.5,依此类推...
So what you need to do is to start a courutine, which will be independent of the fps rate, and will move your GameObject in a constant speed defined by the distance between start-target and the time you want it takes to move from one to the other. 因此,您需要做的是启动一个库鲁特程序,该库特程序与fps速率无关,并将以恒定的速度移动您的GameObject,该速度由起始目标与您希望从一个目标移动到另一个目标之间的时间之间的距离定义。另一个。
public class WolfMovement : MonoBehaviour {
Vector3 start;
Vector3 target;
float lungeSpeed = .8f;
float lungeDistance = 5;
private IEnumerator coroutine;
void Update () {
if(Input.GetKeyDown(KeyCode.M) )
{
start = transform.position;
target = new Vector3(transform.position.x + lungeDistance,transform.position.y , transform.position.z);
coroutine = MoveObject(start,target,lungeSpeed);
StartCoroutine(coroutine);
}
}
IEnumerator MoveObject (Vector3 start, Vector3 target, float speed){
float i = 0.0f;
float rate = 1.0f/speed;
while (i < 1.0) {
i += Time.deltaTime * rate;
transform.position = Vector3.Lerp(start, target, i);
yield return null;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.