【问题标题】:Character won't jump in Unity2D but entered the jump statementUnity2D中角色不会跳转但进入跳转语句
【发布时间】:2016-07-26 06:22:58
【问题描述】:

我在 unity enigne 中的播放器控制脚本 (C#) 有一点问题。我根据玩家的基本动作制定了以下脚本。问题是播放器可以进入跳转语句(调试日志打印出来了) Debug Log 但它不起作用。人物还在地上。 当玩家在地面(接地)并且没有进行二段跳时,将启用跳跃功能。 所以我的问题是是否有任何“代码错误”或者一些我看不到的配置问题?

提前感谢您的帮助!

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);
    }
}

【问题讨论】:

  • 你确定你的对象激活了跳跃的动画吗?
  • 感谢您的评论。问题不在于动画,角色没有改变位置(跳跃)。当动画不起作用时我可以处理它,但不能解决它不改变位置的问题。但这里有另一件奇怪的事情。我把这个小代码放在void update() - 函数中:if (Input.GetKeyDown(KeyCode.Space)) playerRigidbody.AddForce(Vector2.up * jumpHeight); 这也不起作用。
  • 如果你把另一个动画动作加上空格,它会起作用吗?另外,据我所知,在我的主角对象中,我使用了第三人称控制器,并且空间跳跃本身就准备好了

标签: c# unity3d-2dtools


【解决方案1】:

只是在这里猜测,但也许 Vector3.up 不适用于 2D 物理?我不是很喜欢 2D,但你可以试试

playerRigidbody.AddForce(transform.up * jumpHeight);

改为。

另外,您是否尝试过 jumpHeight 的不同值?根据您为刚体设置的质量,5 可能会很小。 并确保您没有限制检查器中的任何轴。

【讨论】:

  • 天啊! @Tarrok 你让我大开眼界!昨天我尝试了 jumpHeigt 的不同设置——都在播放模式下。感谢您的精彩提示。
  • @t3chnico 很高兴我能帮上忙 ;)
【解决方案2】:
// 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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 2022-06-10
    相关资源
    最近更新 更多