简单的问题,但很难找到答案。
我有一个钩子技工,按下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();
}
}
我究竟做错了什么?它应该按预期工作,对吧?
森栏
相关分类