简体   繁体   中英

2d sprite goes too fast with transform.translate in C# script (unity)

Sup. My sprite goes too fast when I press the C button. 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. 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 .

In Lerp the first parameter is the start position and the second the targe. The third parameter is a float between 0 and 1. If it's 0, you get the first parameter in Lerp. If it's 1, you get the second. If 0.5 a middle point between both and so on...

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.

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;
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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