Hook Mechanic 和同步 Time.Delta Time C#

我正在处理一个新项目,其中我的核心机制之一是一个钩子,如果一个物体被击中,它们要么被拉动要么被摧毁。


现在我只是想让这个运动机制工作,这是我的代码:


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 = 5f; //Define float for seconds

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

public int time; 



// Use this for initialization

void Start()

{

    Thrust = 75f;

    rb = GetComponent<Rigidbody>();

    print("working");

}


void OnCollisionEnter(Collision col)

{

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

    {

        print("xd");

        Destroy(col.gameObject);

    }

}



void ThrowHook()

{

    if (Input.GetKeyDown(KeyCode.H) && !moving)

    {

        moving = true;

        var moveIncrement = new Vector3(Thrust, 0, 0 * Time.deltaTime); // setting values? difference between var and int?


        while (time <= HookTravelTime)

        {

            time = Time.deltaTime + 1;// I want the int time to increase by 1 every "second". Cannot impicitly convert type float to iny. An explicit conversion exists.???


            // How do I put VAR MOVEINCREMENT into here? I still don't understand what a var is. Why can't I just make a void()

        }


        if (time >= HookTravelTime)

        {

            transform.position = Vector3.MoveTowards(transform.position, Target.position, Thrust); // return to empty object which is in front of player.

        }


    }

}


// Update is called once per frame

void Update()

    {

        ThrowHook();

    }

}

这些问题是我在代码中注释的,但我将通过它们。


在 Void ThrowHook() 中,我试图将游戏中每一秒的 Int(time) 减少到 +1。我试图将其与 Time.Delta Time 同步,但这不起作用。我不知道我在这里做什么。


在 ThrowHook() 中的 while 循环上方,有人告诉我创建一个 var 来在其中存储信息(Var MoveIncrement)。我试图将恒定运动代码放入其中,但我不知道如何调用它。如果可能的话,你能告诉我这行代码是否错误


请暂时忽略 Bool Moving,因为我一直没有始终如一地试图解决这个问题。


希望我已经把这些问题说清楚了,如果没有,我很抱歉,我急于去橄榄球练习。


TLDR:我想要一个钩子在“时间”还没有超过 5 秒的时候有一个恒定的力量向前。如果有,则钩子移回玩家面前的目标位置。


呼如林
浏览 160回答 1
1回答

ITMISS

Time.deltaTime是完成最后一帧所需的时间(以秒为单位),因此如果您的游戏以 60fps 运行,则其值为 1/60。你可能会一直使用它,因为帧的持续时间不是恒定的,所以试着很好地理解它 https://docs.unity3d.com/ScriptReference/Time-deltaTime.html也就是说,如果你想等待 5 秒来使用 Update() 做某事:float hookTravelTime=5f; // you can use an int if you want full seconds only, you will be able to compare float>int/float<int/eccfloat timeHookTraveling=0f; //in your case the variable named "time"(it isn't a good variable name)bool isHookActive=false;&nbsp; &nbsp; void Update()&nbsp; &nbsp; {//here you should check your input foor hook activation/*if(keyPressed...){&nbsp; &nbsp; what the hook should do on actiovation code here&nbsp; &nbsp; isHookActive=true;&nbsp; &nbsp; }*///if the hook is not resting execute the code below&nbsp; &nbsp; &nbsp; if(isHookActive)&nbsp; &nbsp; &nbsp; &nbsp;{//if the hook traveled for more than hookTravelTime(5 seconds in your case)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(timeHookTraveling>=hookTravelTime)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//your action after the 5 seconds of hook travel&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;timeHookTraveling=0f;//reset the travel time for your next hook activation&nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp;}//if the hook didn't travel for at least 5 seconds, increase its travel time by frame's time, thisone will be executed until you reach value 5.0f&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else//or without else, whatever&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;timeHookTraveling+=Time.deltaTime;//increase your travel time by last frame's time&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }你有问题,因为你正在转换为一个很小的分数(正如我之前所说的,如果你在 60fps 时接近 1/60),所以它的值将始终为 0额外:不要那样检查输入,尝试在函数中添加一些逻辑:如果(输入)然后抛出(所以检查输入然后决定调用抛出)另外我建议你不要通过它的名字访问敌人,如果你将使用一个预制你的敌人的名字将是“敌人(克隆)”,如果会有更多的敌人,ecc?使用图层、标签或类似的东西。
打开App,查看更多内容
随时随地看视频慕课网APP