为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

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 关注
  • 149 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号