变换.位置传送。移动不顺畅

简单的问题,但很难找到答案。


我有一个钩子技工,按下key.B,一个钩子会发射5秒然后应该回来。


这段代码工作正常,只是当分配给召回对象的代码时,它不会顺利恢复,它会传送。这是代码,粗体中的问题特定行


public class Hook : MonoBehaviour

{ //Remember Couroutine is pretty much update()


public Transform Target;

private float Thrust; // Int for motion

public Rigidbody rb;

public float HookTravelTime; //Define float for seconds

bool isHookActive = false;  //Are we currently moving?

public float timeHookTraveling = 0f;





// Use this for initialization

void Start()

{

    Thrust = 75f;

    rb = GetComponent<Rigidbody>();

    float walkspeed = Thrust * Time.deltaTime;

}


void OnCollisionEnter(Collision col)

{

    if (col.gameObject.name == "mob")

    {

        Destroy(col.gameObject);

        print("Other code negated");

        rb.velocity = Vector3.zero;

        transform.position = Vector3.MoveTowards(transform.position, Target.position, Thrust);

        isHookActive = false;

        timeHookTraveling = 0f;

    }

}



void ThrowHook()

{


    if (Input.GetKeyDown(KeyCode.B))

    {

        isHookActive = true;

        rb.AddForce(Vector3.forward * Thrust);

    }

    if (isHookActive )

        {

            if (timeHookTraveling >= HookTravelTime) //if the hook traveled for more than hookTravelTime(5 seconds in your case)

            {


            print("hehemeth");

            rb.velocity = Vector3.zero; //negate addforce from before

          **HERE**  transform.position = Vector3.MoveTowards(transform.position, Target.position, Thrust);

            isHookActive = false;//reset this bool so your Update will not check this script until you don't activate it in your ThrowHook

            timeHookTraveling = 0f;//reset the travel time for your next hook activation

        }


            else//if you havent hit 5 keep increasing

            {

                timeHookTraveling += Time.deltaTime;//increase your travel time by last frame's time

            }

        }

    }




// Update is called once per frame

void Update()

    {

    ThrowHook();

    }



}

我究竟做错了什么?它应该按预期工作,对吧?


ITMISS
浏览 150回答 1
1回答

森栏

Vector3.MoveTowards 需要每帧运行,这就是我* Time.deltaTime在评论中提到的原因。在 5s 时,rb 的速度变为零并isHookActive变为假,因此Vector3.MoveTowards不称为每帧。if (Input.GetKeyDown(KeyCode.B)){&nbsp; &nbsp; isHookActive = true;&nbsp; &nbsp; rb.AddForce(Vector3.forward * Thrust);}if (isHookActive ){&nbsp; &nbsp; if (timeHookTraveling >= HookTravelTime) //if the hook traveled for more than hookTravelTime(5 seconds in your case)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; print("hehemeth");&nbsp; &nbsp; &nbsp; &nbsp; rb.velocity = Vector3.zero; //negate addforce from before&nbsp; &nbsp; &nbsp; &nbsp; isHookActive = false;//reset this bool so your Update will not check this script until you don't activate it in your ThrowHook&nbsp; &nbsp; &nbsp; &nbsp; timeHookTraveling = 0f;//reset the travel time for your next hook activation&nbsp; &nbsp; }&nbsp; &nbsp; else//if you havent hit 5 keep increasing&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; timeHookTraveling += Time.deltaTime;//increase your travel time by last frame's time&nbsp; &nbsp; }}else if (!isHookActive && transform.position != Target.position){&nbsp; &nbsp; transform.position = Vector3.MoveTowards(transform.position, Target.position, Thrust * Time.deltaTime);}一个更好的方法是把if (Input.GetKeyDown(KeyCode.B)){&nbsp; &nbsp; isHookActive = true;&nbsp; &nbsp; rb.AddForce(Vector3.forward * Thrust);}进入FixedUpdate()但不是Update()同时if (isHookActive ){... }留在Update().
打开App,查看更多内容
随时随地看视频慕课网APP