如何双跳我的平台游戏代码?

时间:2013-12-20 14:34:21

标签: c# unity3d

我正在制作一个简单的2D平台游戏。我已经成功地让我的盒子从左到右移动,并在平台之间跳转等。

我希望能够做到的是进行双跳,在半空中你可以再次按空格键再次跳到半空中。

我已经尝试了以下代码,它似乎对我不起作用。我刚刚开始使用Unity,我真的非常喜欢它,但是我可以使用一些建议,我应该如何改变我的代码中的一些东西来执行双跳。任何建议将不胜感激。

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(playerpysics))]
public class PlayerController : MonoBehaviour
{

    //Player Handling
    public float gravity = 20;
    public float speed = 8;
    public float acceleration = 25;
    public float jumpHeight = 12;
    private float doubleJump = 10;
    private float currentSpeed;
    private float targetSpeed;
    private Vector2 amountToMove;
    private playerpysics playerpysics;

    // Use this for initialization

    void Start ()
    {
        playerpysics = GetComponent<playerpysics> ();
    }

    // Update is called once per frame
    void Update ()
    {
        if (playerpysics.movementStopped) 
        {
            targetSpeed = 0;
            currentSpeed = 0;
        }

        targetSpeed = Input.GetAxisRaw ("Horizontal") * speed;
        currentSpeed = IncrementTowards (currentSpeed, targetSpeed, acceleration);

        if (playerpysics.grounded) 
        {
            amountToMove.y = 0;

            if (Input.GetButtonDown ("Jump")) 
            {
                amountToMove.y = jumpHeight;
            } 
            else if (Input.GetButtonDown * 2 ("Jump"))
                amountToMove.y = doubleJump; 
        }

        amountToMove.x = currentSpeed;
        amountToMove.y -= gravity * Time.deltaTime;
        playerpysics.Move (amountToMove * Time.deltaTime);
    }

    private float IncrementTowards (float n, float target, float a)
    {
        if (n == target) 
        {
            return n;
        } 
        else 
        {
            float dir = Mathf.Sign (target - n);
            n += a * Time.deltaTime * dir;
            return (dir == Mathf.Sign (target - n)) ? n : target;
        }
    }
}

3 个答案:

答案 0 :(得分:1)

在我所有的统一平台游戏中,我尝试用最简单的可行方式...基本上我有一个int来计算当前的跳跃次数,以及一个用于最大跳跃的int

int jumpcount;
int maxjumps;

Public void Jump() {
    If (jumpcount >= maxjumps) {
        return;
    } 
    ... /whatever does a jump here
    Jumpcount++
 } 
//this will be called when physics calls a  Collision
void OnCollisionEnter (Collision col){
    Jumpcount = 0;

} 

see OnCollisionEnter info here

通过添加一个检查以查看它碰撞的物体类型,允许墙壁跳跃,杀手层等等,可以进一步改进此脚本。

我使用简单的if(col.GameObject.Layer = somelayer)执行此操作,但实际上没有正确的方法来检查

答案 1 :(得分:0)

我还需要我的角色双跳。但是,第二次跳跃必须“更弱”。 我这样解决了:

public bool jump;
float jumpForce = 0f; // Force added when the player jumps.
public float firstJumpForce = 0f;
public float secondJumpForce = 0f;

private bool canJump = false;
private int availableJumps = 2;

void Update () {

    if ((Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown (0))) {
        jump = true; // I can jump
    }
}

//Update for physics steps. Regular intervals. 
void FixedUpdate(){

    if(jump && canJump)
    {
        // Add a vertical force to the player.
        rigidbody2D.AddForce(new Vector2(0f, jumpForce));

        // Can the player jump again??
        availableJumps--;
        if (availableJumps <= 0){
            canJump = false;
        } else {
            //I modify the second jump force
            jumpForce = secondJumpForce;
        }
        jump = false;
    }
}

void OnCollisionEnter2D(Collision2D coll) {
    if (coll.gameObject.tag == "Ground") {
        canJump = true;
        availableJumps = 2;
        jumpForce = firstJumpForce;
    }
}

对我有用!

答案 2 :(得分:0)

牛蒡谢谢你的回答!我在我的角色上尝试了你的代码,除了当我使用return for

时,它的效果很好
    public void Jump () {
        if (jumpCount >= maxJumps){
            return;
    }
}

我发现它只让我做跳跃序列一次,所以我做了

public void Jump (bool jump) {
        if (jumpCount >= maxJumps){
            jump = false;
    }
}

然后在下面我检查了我是否接地,然后我将设置jumpCount = 0和jump = true。

谢谢你,我一直在试图弄清楚如何使用Unity PlatformerCharacter2D脚本进行双跳。