在Unity中通过AddForce跳跃

huangapple go评论60阅读模式
英文:

Jumping in Unity by AddForce

问题

我最近开始在Unity中使用物理学,我需要让我的角色跳跃。我想用AddForce()来实现,但我也需要检查角色是否站在地面上。在代码中,我通过时间来进行检查。这是我的代码:

private void JumpByForce()
{
    time_after_jump = Time.time + 1f;
    Debug.Log(time_after_jump);

    if (Time.time > time_after_jump)
    {
        isJumping = true;
    }
    if (isJumping)
    {
        Player.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 750));
        isJumping = false;
    }
}

private void Start()
{
    Player_pos_x = Player.transform.position.x;
    Player_pos_y = Player.transform.position.y;
    isJumping = true;
}

我以为我写了正确的代码,但在console中没有出现我想要的内容。它打印出time_after_jump而没有添加+ 1f。第一次点击跳跃按钮时,玩家会正确跳跃。但之后它不会跳跃。我最大的问题是 - 为什么time_after_jump打印出来时没有加上(+ 1f)?

英文:

I'm recently started working with physics in Unity and I need to let my character jump.
I want to do it by AddForce() but I also need to check is the character grounded.
In the code I'm checking it with time.
Here is my code:

private void JumpByForce() 
{
    time_after_jump = Time.time + 1f;
    Debug.Log(time_after_jump);

    if (Time.time &gt; time_after_jump)
    {
        isJumping = true;
    }
    if (isJumping)
    {
        Player.GetComponent&lt;Rigidbody2D&gt;().AddForce(new Vector2(0, 750));
        isJumping = false;
    }
}

private void Start()
{
    Player_pos_x = Player.transform.position.x;
    Player_pos_y = Player.transform.position.y;
    isJumping = true;
    
}

I thought that I wrote a right code, but in console isn't appearing what I want.
(It's printing time_after_jump without adding + 1f).
At the first time when I'm clicking on the jump button the Player Jumping correctly.But the following times it isn't jumping.
My biggest question Is - Why time_after_jump printing without
(+ 1f)?

答案1

得分: 0

Debug.Log() 会输出最终值,而不是变量中的表达式。在你的情况下,它会输出 time_after_jump 的最终值。Unity 在上一行计算了该值,然后打印出来(所以它打印的是 10+1 而不是计算结果 11)。

此外,你的玩家在第一次跳跃后不再跳跃的原因是 Time.time 永远不会大于 time_after_jump,因为在检查之前你将其加1,因此第一个 if 从未触发,因此 isJumping 除了第一次初始化为 true 外永远不会为 true。

英文:

Debug.Log() prints the final value, not the expression in the variable. In your situation, it prints the final value of time_after_jump. Unity calculates that value on the previous line and then prints it (So instead of printing 10+1 it prints 11).

Also, the reason your player doesn't jump after the first time is because Time.time will never be greater than time_after_jump since you are adding 1 to it before the check, therefore the first if is never triggered, and therefore isJumping is never true apart from the first time when you initialize it with the value true.

答案2

得分: 0

我相信我已经找到了这里的问题。if(Time.time > time_after_jump) 总是会返回 false

假设 Time.time = 2。而 time_after_jump = Time.time + 1。然后 time_after_jump = 3。这意味着它始终更大。

请尝试这个代替:

time_after_jump = Time.time;
Debug.Log(time_after_jump);

if ((Time.time + 1f) > time_after_jump)
{
    isJumping = true;
}

尽管个人而言,我会使用 time.deltaTime,创建一个计时器,并在跳跃时重置计时器的值,以便将 isJumping 设置为 false。

例如:

time_after_jump += Time.deltaTime; 
if(time_after_jump > timeJumping)
{
  isJumping = true;
}

if(isJumping)
{
   //Movement here
   time_after_jump = 0;
   isJumping = false;
}

然而,如果你使用输入来实现跳跃,这将更简单。然后,你会在按下跳跃键时重置计时器。

我在工作中,所以这未经测试,但我希望你明白我的意思。愉快编码。

英文:

I believe I have figured out the problem here. if(Time.time &gt; time_after_jump) while always return false.

Let's say Time.time = 2. And time_after_jump = Time.time + 1. Then time_after_jump = 3. Meaning it is always greater.

Try this instead:

time_after_jump = Time.time;
Debug.Log(time_after_jump);

if ((Time.time + 1f) &gt; time_after_jump)
{
    isJumping = true;
}

Although, personally I would use time.deltaTime, create a timer, and reset the timer value when jumping, so as to set isJumping to false.

E.g:

time_after_jump += Time.deltaTime; 
if(time_after_jump &gt; timeJumping)
{
  isJumping = true;
}

if(isJumping)
{
   //Movement here
   time_after_jump = 0;
   isJumping = false;
}

However, this would be a lot simpler if you used an Input to get the jump. Then you would reset the timer when jump is pressed.

Am at work, so this is untested, but I hope you get the idea. Happy coding.

答案3

得分: 0

使角色通过AddForce在时间上跳跃,我必须在FixedUpdate方法中检查'time_after_jump',如果'Time.time'大于'time_after_jump',则'isJumping'将为true,我可以再次使角色跳跃。

我使用了这段代码:

private void JumpByForce() 
{
    if (isJumping)
    {
        time_after_jump = Time.time + 0.8f;
        Player.GetComponent&lt;Rigidbody2D&gt;().AddForce(new Vector2(0, 750));
        isJumping = false;
    }
}

private void FixedUpdate()
{
    if (Time.time &gt; time_after_jump)
    {
        isJumping = true;
    }
}
英文:

To make the character jump by AddForce with time I've must check time_after_jump in FixedUpdate method and if Time.time will more than time_after_jump, than isJumping will be true and I can again jump our character.

I used this code:

private void JumpByForce() 
{
    if (isJumping)
    {
        time_after_jump = Time.time + 0.8f;
        Player.GetComponent&lt;Rigidbody2D&gt;().AddForce(new Vector2(0, 750));
        isJumping = false;
    }
}

private void FixedUpdate()
{
    if (Time.time &gt; time_after_jump)
    {
        isJumping = true;
    }
}

huangapple
  • 本文由 发表于 2023年4月4日 13:43:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定