簡體   English   中英

角色不會在Unity2D中跳轉,但輸入了跳轉語句

[英]Character won't jump in Unity2D but entered the jump statement

我的統一播放器中的播放器控制腳本(C#)有點問題。 我根據播放器的基本動作制定了以下腳本。 問題是播放器可以輸入跳轉語句(將調試日志打印出來)和調試日志,但是它將不起作用。 角色仍然在地面上。 當播放器在地面上(接地)並且沒有兩次跳躍時,將啟用跳躍功能。 所以我的問題是是否有任何“代碼錯誤”或某些我看不到的配置問題?

提前謝謝你的幫助!

using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour
{

    // public variables
    public float speed = 3f;
    public float jumpHeight = 5f;

    // private variables
    Vector3 movement;
    Animator anim;
    Rigidbody2D playerRigidbody;

    // variables for the ground check
    public Transform groundCheck;
    public float groundCheckRadius;
    public LayerMask whatIsGround;
    private bool grounded;
    private bool doubleJump;

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        // Proves if the player is on the ground and activate the double jump function
        if (grounded)
        {
            doubleJump = false;
        }

        // First line of proving the jump
        if (Input.GetMouseButtonDown(0) && grounded)
        {
            Debug.Log("Jump if entered");
            Jump();
        }

        if (Input.GetMouseButtonDown(0) && !doubleJump && !grounded)
        {
            Debug.Log("double Jump");
            Jump();
            doubleJump = true;
        }


        // Flipping the Player when he runs back
        if (Input.GetAxis("Horizontal") < 0)
        {
            playerRigidbody.transform.localScale = new Vector2(-1.7f, 1.7f);
        }

        else
        {
            playerRigidbody.transform.localScale = new Vector2(1.7f, 1.7f);
        }
    }

    void Awake()
    {
        // References setting up
        playerRigidbody = this.GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    void FixedUpdate()
    {
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");

        // simple Movement without a speed control
        Move(horizontal, vertical);
        Animating(horizontal, vertical);

        // Section for ground detection
        grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);

        // Set the parameter for the jump animation false or true
        anim.SetBool("Grounded", grounded);
    }

    void Move(float horizontal, float vertical)
    {
        movement.Set(horizontal, 0f, vertical);
        movement = movement.normalized * speed * Time.deltaTime;

        playerRigidbody.MovePosition(transform.position + movement);
    }

    void Jump()
    {
        playerRigidbody.AddForce(Vector3.up * jumpHeight);
        // playerRigidbody.AddForce(new Vector2(0f, jumpHeight), ForceMode2D.Impulse);
        Debug.Log("Jump function");
    }

    void Animating(float h, float v)
    {
        bool walking = h != 0f || v != 0f;
        anim.SetBool("IsWalking", walking);
    }
}

只是在這里猜測,但是Vector3.up可能不適用於2D物理嗎? 我不太喜歡2D,但您可以嘗試

playerRigidbody.AddForce(transform.up * jumpHeight);

代替。

另外,您是否為jumpHeight嘗試了其他值? 根據您為剛體設置的質量,5可能很小。 並確保您沒有限制檢查器中的任何軸。

// Note:  If you want the object to move in a reliable predictable way but still allow physics interactions, use MovePosition (set the object to kinematic if you want it to be unaffected by physics but still be able to affect other things, and uncheck kinematic if you want both objects to be able to be acted on by physics. 

If you want to move your object but let physics handle the finer details, add a force.

playerRigidbody.rigidbody2D.AddForce(Vector3.up * 10 * Time.deltaTime); 

use Vector.up, Vector.down, vector.right, Vectore.left along with time.deltaTime to have smooth movement along the frame.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM