我正在处理一个新项目,其中我的核心机制之一是钩子,如果物体被击中,则钩子会被拉扯或破坏。
就目前而言,我只是想让这个运动机械师正常工作, 这是我的代码:
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)。我试图在其中添加恒定运动代码,但是我不知道如何调用它。如果可能的话,您能告诉我这行代码是否错误
请暂时忽略“布尔运动”,因为我再次不一致地试图解决此问题。
希望我已将这些问题弄清楚了,如果没有解决,我很抱歉,我急于去橄榄球练习。
TLDR:我希望钩子在“时间”还没有过去5秒钟的情况下向前施加恒定的力。如果有,那么挂钩将移回播放器前面的目标位置。
谢谢:P
答案 0 :(得分:1)
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/ecc
float timeHookTraveling=0f; //in your case the variable named "time"(it isn't a good variable name)
bool isHookActive=false;
void Update()
{
//here you should check your input foor hook activation
/*
if(keyPressed...)
{
what the hook should do on actiovation code here
isHookActive=true;
}
*/
//if the hook is not resting execute the code below
if(isHookActive)
{
//if the hook traveled for more than hookTravelTime(5 seconds in your case)
if(timeHookTraveling>=hookTravelTime)
{
//your action after the 5 seconds of hook travel
timeHookTraveling=0f;//reset the travel time for your next hook activation
isHookActive=false;//reset this bool so your Update will not check this script until you don't activate it in your ThrowHook.
}
//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
else//or without else, whatever
{
timeHookTraveling+=Time.deltaTime;//increase your travel time by last frame's time
}
}
}
您遇到了问题,因为您将整数转换为整数(如我之前所说,如果您以60fps的速度接近1/60,那么它的值将始终为0
额外:不要以这种方式检查输入,请尝试在函数中添加一些逻辑:if(输入)然后throw(所以检查输入然后决定调用throw) 我也建议你不要用敌人的名字来访问敌人,如果你要使用一个预制件,你的敌人的名字将是“敌人(克隆)”,如果还有更多的敌人,怎么办?使用图层,标签或类似的东西。