亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

變換.位置傳送。移動不順暢

變換.位置傳送。移動不順暢

C#
ITMISS 2022-01-16 18:14:30
簡單的問題,但很難找到答案。我有一個鉤子技工,按下key.B,一個鉤子會發射5秒然后應該回來。這段代碼工作正常,只是當分配給召回對象的代碼時,它不會順利恢復,它會傳送。這是代碼,粗體中的問題特定行public class Hook : MonoBehaviour{ //Remember Couroutine is pretty much update()public Transform Target;private float Thrust; // Int for motionpublic Rigidbody rb;public float HookTravelTime; //Define float for secondsbool isHookActive = false;  //Are we currently moving?public float timeHookTraveling = 0f;// Use this for initializationvoid 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 framevoid Update()    {    ThrowHook();    }}我究竟做錯了什么?它應該按預期工作,對吧?
查看完整描述

1 回答

?
森欄

TA貢獻1810條經驗 獲得超5個贊

Vector3.MoveTowards 需要每幀運行,這就是我* Time.deltaTime在評論中提到的原因。


在 5s 時,rb 的速度變為零并isHookActive變為假,因此Vector3.MoveTowards不稱為每幀。


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

        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

    }

}


else if (!isHookActive && transform.position != Target.position)

{

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

}

一個更好的方法是把


if (Input.GetKeyDown(KeyCode.B))

{

    isHookActive = true;

    rb.AddForce(Vector3.forward * Thrust);

}

進入FixedUpdate()但不是Update()同時


if (isHookActive )

{... }

留在Update().


查看完整回答
反對 回復 2022-01-16
  • 1 回答
  • 0 關注
  • 147 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號