繁体   English   中英

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

[英]transform.position teleporting. Not smoothly moving

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

我有一个钩子机械师,在按下 key.B 时,一个钩子会发射 5 秒然后应该回来。

这段代码工作正常,只是当分配给调用对象的代码时,它不会顺利返回,而是传送。 这是代码, BOLD 中特定于问题的行

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


}

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

每帧都需要运行 Vector3.MoveTowards,这就是我在评论中提到* Time.deltaTime的原因。

在 5 秒时,rb 的速度变为 0, isHookActive变为 false,因此Vector3.MoveTowards不是每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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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