【问题标题】:My 2D Character is not jumping at all我的 2D 角色根本不跳
【发布时间】:2017-09-18 02:14:39
【问题描述】:

我的角色有问题,根本不会跳跃。我是 Unity 新手,但我确保将脚本应用于播放器并调整速度,我没有接触 Rigidbody 2D。如果有人可以帮助我解决我们的问题,将不胜感激。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour {

public float moveSpeed;
public float jumpSpeed;
public bool grounded = false;
private Rigidbody2D rb;

void Start() {
    rb = GetComponent<Rigidbody2D>();
}

void Update () {

    transform.Translate (Input.GetAxisRaw ("Horizontal") * moveSpeed * Time.deltaTime, 0, 0);

    if (grounded) 
    {
        if (Input.GetButtonDown ("Jump"))
        {
            rb.AddForce (Vector2.up * jumpSpeed);
            grounded = false;
        }
    }
}


void OnCollisionEnter2D (Collision2D coll){
    if (coll.transform.tag == "Ground") 
    {
        grounded = true;
    }
}

}

Player GameObject 的 Inspector 窗口

Ground GameObject 的 Inspector 窗口

【问题讨论】:

    标签: c# unity2d rigid-bodies


    【解决方案1】:

    你的问题是你没有这样标记地面游戏对象。所以在OnCollisionEnter2D 中,角色检测到了碰撞,但if (coll.transform.tag == "Ground") 永远不会是真的。所以这意味着角色不能是grounded

    因为接地是检查玩家是否按下跳跃键的第一个条件。它不可能永远跳下去

    if (grounded) 
        {
            if (Input.GetButtonDown ("Jump"))
            {
                rb.AddForce (Vector2.up * jumpSpeed);
                grounded = false;
            }
        }
    

    要解决这个问题:您需要将地面游戏对象标记为这样。如果您不确定如何执行此操作,请在标签菜单上创建(如果它不存在)一个名为 Ground 的新标签。然后在同一个菜单中将 Ground Tag 分配给 Ground GameObject。在这里,您可以了解如何在需要视觉参考的情况下:

    https://docs.unity3d.com/Manual/Tags.html

    编辑:如果一切都失败了,你可以试试这个脚本。它应该工作。前段时间我使用了自己,我清理了代码,以便只留下在 x 和 y 轴上移动角色所需的内容。希望已包含您需要的所有内容:

    public class CharacterController2D : MonoBehaviour {
    
        // LayerMask to determine what is considered ground for the player
        public LayerMask whatIsGround;
    
        // Transform just below feet for checking if player is grounded
        public Transform groundCheck;
    
        // store references to components on the gameObject
        Transform transform;
        Rigidbody2D rigidbody;
    
        bool isGrounded = false;
    
        float vy;
        float vx;
    
        public float jumpForce = 600f;
    
        void Awake () {
            transform = GetComponent<Transform> ();
            rigidbody = GetComponent<Rigidbody2D> ();
        }
    
    
        void Update()
        {
    
            // determine horizontal velocity change based on the horizontal input
            vx = Input.GetAxisRaw ("Horizontal");
            vy = rigidbody.velocity.y;
    
            // Check to see if character is grounded by raycasting from the middle of the player
            // down to the groundCheck position and see if collected with gameobjects on the
            // whatIsGround layer
            isGrounded = Physics2D.Linecast(transform.position, groundCheck.position, whatIsGround);
    
            if(isGrounded && Input.GetButtonDown("Jump")) // If grounded AND jump button pressed, then allow the player to jump
            {
                DoJump();
            }
    
            // Change the actual velocity on the rigidbody
            rigidbody.velocity = new Vector2(_vx * MoveSpeed, _vy);
    
        }
    
        //Make the player jump
        void DoJump()
        {
            // reset current vertical motion to 0 prior to jump
            vy = 0f;
            // add a force in the up direction
            rigidbody.AddForce (new Vector2 (0, jumpForce));
    
        }
    }
    

    所以要考虑的事情:

    • 您无需标记地面,而是创建一个包含所有内容的图层 考虑接地。这将包括角色的可能平台 可能会跳过。将此层作为参数传递给 督察

    • 您需要在角色的脚上放置一个空游戏对象。 您将在编辑器中将该游戏对象拖放到 groundCheck 公共变量。

    • 您将使用 Physics2D.Linecast 而不是 OnTriggerEnter 将从角色的位置到其下方移动一条线 脚(你应该把Transform中提到的 上一步),如果在中间有一个元素 groundLayer,表示角色将被接地。

    如果有任何不清楚的地方或发现错误,请告诉我。

    【讨论】:

    • 感谢您的回复,但我现在确实尝试过,但没有成功。
    • 我会从头开始重做,看看是否可行。
    • @KonoDioDa 我用脚本编辑了我的答案,您可以用作参考。
    • 感谢您的脚本,但我设法在 Unity 中重做了一些部分,并更改了脚本周围的内容以使用不同的东西,并让它工作。
    • @KonoDioDa 我很高兴你做到了。如果您接受我的回答,我将不胜感激
    【解决方案2】:

    如前所述,您的问题显然是您缺少标记地面对象:)

    提示:当我遇到此类问题时,我喜欢做的是使用统一的 Debug.Log() 来定位问题所在。它会让您在控制台中轻松知道哪些代码正在运行,哪些未运行。尝试执行以下操作:

    无效更新(){

    transform.Translate (Input.GetAxisRaw ("Horizontal") * moveSpeed * Time.deltaTime, 0, 0);
    
    if (grounded) 
    {
        Debug.Log("Is grounded");
    
        if (Input.GetButtonDown ("Jump"))
        {
            Debug.Log("Jump clicked");
            rb.AddForce (Vector2.up * jumpSpeed);
            grounded = false;
        }
    }
    

    }

    【讨论】:

    • 感谢您的提示,但至于标签,我现在确实尝试过,但没有成功。
    • 同样从调试开始,它说它是接地的,当我按下跳转时,它说“按下跳转”但显然它没有上升或下降:(但是,我'那就从头开始重做吧
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多