无法正确跳墙

时间:2018-11-08 07:45:06

标签: c# unity3d 2d game-physics

我正试图让我的球员从墙上跳下来。我知道我可以通过反方向加力来做到这一点。但是我的播放器跳得很奇怪,我尝试通过阅读和观看教程来解决它,但我自己无法做到。我认为我需要更专业和经验丰富的头脑来解决这个问题。

我有2个脚本,一个负责墙跳,另一个脚本负责移动和从地面跳。在此脚本中,如果按下按钮的时间更长,则玩家会跳得更高。

这是PlayerScript

public class PLayer : MonoBehaviour {


    float input;
    public float moveSpeed = 10;
    bool jumpButtonPressed = false;

    float startingMoveSpeed;
    [Tooltip("move Speed when player jump")]
    public float jumpMoveSpeedDecrementor = 3;
    public float jumpForce = 10;
    Rigidbody2D rgbd;
     float groundCheckRadius = 0.1f;
    [HideInInspector]
    public bool grounded,wallJump;
    bool isJumping;



    // Use this for initialization
    void Start () {

        startingMoveSpeed = moveSpeed;
        wallJump = false;
    }
    private void Update()
    {
        if (!jumpButtonPressed)
        {
            jumpButtonPressed = Input.GetKeyDown(KeyCode.Z);
        }


    }
    // Update is called once per frame
    void FixedUpdate ()
    {


       //check if grounded or doing wall jump
        if (grounded|| wallJump)
        {
            animator.SetFloat("horizontalSpeed", Mathf.Abs(input));
            moveSpeed = startingMoveSpeed;
        }
        else
        {//if in the air mean jumping
            moveSpeed = jumpMoveSpeedDecrementor;
        }



       //if jump button press then jump
        if (jumpButtonPressed && grounded )
        {  
            isJumping = true;
            animator.SetTrigger("jump");
            rgbd.AddForce(Vector2.up * jumpForce*Time.deltaTime,ForceMode2D.Impulse);

            jumpButtonPressed = false;
        }

        }
        if (Input.GetKeyUp(KeyCode.Z))
        {
            isJumping = false;
        }
    }



}

这是处理壁面跳动的第二个脚本。我在壁上添加了0.02f摩擦的物理材料。

public class WallJump : MonoBehaviour {
    PLayer player;
    public float rayDistance;
    public int speed = 20;
    public int speedY=300;
    public LayerMask playerLayer;
    // Use this for initialization
    void Start () {
        player = GetComponent<PLayer>();
    }

    // Update is called once per frame
    void Update () {
       //ray should not collide with the self
        Physics2D.queriesStartInColliders= false;
   //ray cast
          RaycastHit2D hit = Physics2D.Raycast(transform.position,Vector2.right*transform.localScale.x, rayDistance);
            //if hit wall and jump
            if (Input.GetKeyDown(KeyCode.Z ) && !player.grounded && hit.collider.tag=="wall")
            {
            player.wallJump = true;
            Debug.Log("adding force");
   //add force in -X and y direction
            GetComponent<Rigidbody2D>().AddForce(new Vector2(speedY*hit.normal.x,speedY),ForceMode2D.Impulse);
            //  GetComponent<PLayer>().flipUsingScale();
            }
        else
         {
            player.wallJump = false;
         }


    }
    private void OnDrawGizmos()
    {
        Gizmos.color = Color.green;
        Gizmos.DrawLine(transform.position, transform.position+ Vector3.right * transform.localScale.x * rayDistance);

    }
}

感谢您阅读和帮助像我这样不成熟的程序员。

Jump error

0 个答案:

没有答案