让玩家跳至Y轴。

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

How to make the player jump to the Y axis

问题

I understand your instructions. Here is the translated code part:

所以我有一个玩家,我可以用滑块来倾斜。当你把滑块向右拉得越多,他就会向右旋转,直到达到50度的限制,向左也是同样的道理。

所以我的问题是,我如何制作一个UI按钮,使玩家跳跃到他旋转的方向,并使他停在跳跃落地的位置。当然还要考虑地面检查,这样玩家就不会在空中多次跳跃。

我已经尝试过一段代码,但是当玩家在地面上跳跃时,他会继续沿着跳跃方向滑动。

我是Unity的新手,如果你能给我提供示例代码或者至少是能帮助我的好教程,那就太好了。

编辑:有人告诉我我的问题太宽泛了,所以这里有更多的信息。

我使用了这段代码`Vector2 direction = transform.up;`来计算y方向,然后使用`rb.AddForce(direction * jumpForce, ForceMode2D.Impulse);`来跳到计算出的方向。

这段代码的问题是,玩家在落地后继续在地面上滑动,所以在另一个问题中,有人建议我增加地面摩擦力,并给了我这段代码:

```csharp
private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.CompareTag("Ground"))
    {
        Vector2 counterForce = -rb.velocity.normalized * jumpForce / 2f;

        rb.AddForce(counterForce, ForceMode2D.Impulse);

        rb.velocity *= groundFriction;
    }
}

但不幸的是,这段代码也没有起作用,我甚至认为它使情况变得更糟。玩家开始朝着相反的方向滑动,而且滑得非常快、非常远。

所以,有人知道更好的代码或者修复这段代码的方法吗?


<details>
<summary>英文:</summary>

so I have a player that I can tilt with a slider. So the more you pull the slider to the right, the more he will rotate to the right till he reaches the limit of 50 degrees, and the same thing for left. 

So my question is, how do I make a UI button which will make the player jump to the direction he is rotated to, and make him stop where he landed. Also with ground check of course, so the player won&#39;t jump multiple times in the air

I already tried a code, but it made the player continue sliding to the direction he jumped to when he was on the ground. 

Also I&#39;m new to unity, so it would be great if you would give me an example code or at least good tutorials that would help me out. 

EDIT: I was informed that my question is too broad, so here some more information. 

I used the code `Vector2 direction = transform.up;` to calculate the y direction and then `rb.AddForce(direction * jumpForce, ForceMode2D.Impulse);` to jump to the calculated direction. 

The issue with this code was, that the player kept sliding on the ground after the landing, so in another question someone suggested me to increase ground friction and gave me this code:

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag(&quot;Ground&quot;))
        {
            Vector2 counterForce = -rb.velocity.normalized * jumpForce / 2f;

            rb.AddForce(counterForce, ForceMode2D.Impulse);

            rb.velocity *= groundFriction;
        }
    }

But unfortunately this code didn&#39;t work either, I would say it made it even worse. The player started sliding in the opposite direction and slide really fast and far away. 

So, does someone know a better code or a way to fix this code? 


</details>


# 答案1
**得分**: 1

以下是翻译好的部分:

**以下是一些实现跳跃机制的步骤:**
-----------------

 1. 创建一个新的**公共**函数(例如,public void test(){})。
 2. 将Rigidbody2D添加到玩家游戏对象中。
 3. 在脚本中,通过写入"public RigidBody2D rb"来引用Rigidbody2D,并将玩家游戏对象拖到脚本中弹出的字段中。
 4. 在您创建的公共函数中,写入rb.AddForce(transform.up * [jumpForceNumber])。
 5. 使用代码中的地面检测部分(假设它有效)创建一个if语句,以使即使调用函数,玩家也必须触碰地面。
 6. 将脚本附加到玩家上,转到检查器中的按钮,滚动检查器,找到OnClick功能。单击加号图标,附加您的玩家游戏对象,找到您的玩家脚本的名称在弹出的字段中,然后选择您的新公共函数。

**关于您提到玩家“滑动”的部分:**

在Rigidbody2D中,尝试调整线性阻尼(增加它),而不是应用反作用力。按照Unity的定义,线性阻尼是物体由于与周围空气或水的摩擦而减速的倾向。如果这不起作用,可能是整个Rigidbody2D的问题,因为它的工作方式是您朝向旋转的方向具有更大的质量,但我假设您已经冻结了Rigidbody2D的旋转约束,因此它在侧面摔倒的尝试中难以响应,所以它试图以不同的方式移动。

**另一种方法:**

 1. 创建一个空的游戏对象,并将玩家置于其下。
 2. 从原始玩家中删除玩家控制器脚本、碰撞器和Rigidbody2D,并将它们添加到新的父对象中。
 3. 创建一个名为"RotationHandler"的新脚本,将玩家控制器的旋转部分移到新脚本中。将新脚本附加到原始玩家并将其连接到滑块上。
 4. 在playercontroller中写入"private Vector3 playerRotation"。然后在update()函数中写入:

    playerRotation = GameObject.Find("OriginalPlayerNameHere").transform.eulerAngles

 5. 回到您写的"rb.AddForce(transform.up * [jumpForceNumber]);"部分,并将"transform.up"替换为"playerRotation"。

希望这能帮助您。

<details>
<summary>英文:</summary>

**Here are some steps you can take to implement jumping mechanics with a button.**
-----------------

 1. Make a new **public** function (For example public void test (){}).
 2. Add a rigidbody2d to the player game object
 3. In the script, refrence the rigidbody2d by writing &quot;public RigidBody2d rb&quot; and dragging the player game object to the field that pops up in the script.
 4. In the public function you made, write rb.AddForce(transform.up * [jumpForceNumber]);.
 5. Make an if statement using the ground check part of the code (assuming it works), to make it so that even if the function is called, the player has to be touching the ground.
 6. Attach the script to the player, go to your button in the inspector, scroll down in the inspector, and locate the OnClick function. Click the plus icon, attach your player game object, find the name of your player script in the field that pops up, then select your new public function.

**FOR WHERE YOU SAID THE PLAYER &quot;SLIDES&quot;:**

In your rigidbody2d, try playing around with the linear drag (increasing it) instead of applying a counterforce. Linear drag, the way unity defines it, is the tendency of an object to slow down due to friction with the air or water that surrounds it. If this doesn&#39;t work, it might be the issue of the rigidbody2d as a whole, because the way it works is that you have more mass in the direction you are rotated to, but since I assume you froze the rotational constraints on your rigidbody2d, it is struggling to respond by falling on it is side, so its attempt to move as an alternate.

**AN ALTERNATE ROUTE:**

 1. Create an empty game object, and parent the player to it

 2. remove the player controller script, collider, and rigidbody2d from the original player and add them to the new parent.
 
 3. Make a new script called &quot;RotationHandler&quot; and move the rotation part of the playercontroller to the new script. Attach the new script to the original player and connect it to the slider.

 4. Write this in playercontroller: &quot;private Vector3 playerRotation&quot; in the beginning. Then in the update() function write

    playerRotation = GameObject.Find(&quot;OriginalPlayerNameHere&quot;).transform.eulerAngles

 5. Go back to where you wrote &quot;rb.AddForce(transform.up *
 [jumpForceNumber]);&quot; and replace &quot;transform.up&quot; with
 &quot;playerRotation&quot;

Hope this helps

</details>



huangapple
  • 本文由 发表于 2023年5月8日 01:04:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195247.html
匿名

发表评论

匿名网友

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

确定