【问题标题】:Stop Player/User from multi jump in my Unity game阻止玩家/用户在我的 Unity 游戏中进行多跳
【发布时间】:2014-10-30 19:53:10
【问题描述】:

您好,感谢您阅读这篇文章。

我是 Unity 的新手,但无论如何我还是设法制作了一款小型 2d 游戏。但是我在跳转功能上遇到了一点问题。

玩家/用户不应该能够在游戏中进行多跳。

这是控制播放器的 C# 脚本。

using UnityEngine;
using System.Collections;

public class RobotController : MonoBehaviour {
//This will be our maximum speed as we will always be multiplying by 1
public float maxSpeed = 2f;
public GameObject player;
//a boolean value to represent whether we are facing left or not
bool facingLeft = true;
//a value to represent our Animator
Animator anim;
//to check ground and to have a jumpforce we can change in the editor
bool grounded = true;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float jumpForce = 700f;

// Use this for initialization
void Start () {
    //set anim to our animator
    anim = GetComponent <Animator>();

}


void FixedUpdate () {
    //set our vSpeed
    anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
    //set our grounded bool
    grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
    //set ground in our Animator to match grounded
    anim.SetBool ("Ground", grounded);


    float move = Input.GetAxis ("Horizontal");//Gives us of one if we are moving via the arrow keys
    //move our Players rigidbody
    rigidbody2D.velocity = new Vector3 (move * maxSpeed, rigidbody2D.velocity.y);   
    //set our speed
    anim.SetFloat ("Speed",Mathf.Abs (move));
    //if we are moving left but not facing left flip, and vice versa
    if (move > 0 && !facingLeft) {

        Flip ();
    } else if (move < 0 && facingLeft) {
        Flip ();
    }
}

void Update(){
    //if we are on the ground and the space bar was pressed, change our ground state and add an upward force
    if(grounded && Input.GetKeyDown (KeyCode.UpArrow)){
        anim.SetBool("Ground",false);
        rigidbody2D.AddForce (new Vector2(0,jumpForce));
    }


}

//flip if needed
void Flip(){
    facingLeft = !facingLeft;
    Vector3 theScale = transform.localScale;
    theScale.x *= -1;
    transform.localScale = theScale;
}
}

这里是 Player 对象和 GroundCheck 对象。

如何阻止玩家进行多重跳跃。因此,如果他按向上箭头键,他会跳跃,并且在着陆之前无法再次跳跃。 感谢您的时间和帮助

更新

如果很难看到这里的图片是 Imgur 上的图片: http://imgur.com/GKf4bgi,2i7A0AU#0

【问题讨论】:

  • 您是否尝试将跳转代码移动到FixedUpdate 而不是Update?它可能会产生一些影响,因为这是您检查播放器是否接地的地方。

标签: unity3d


【解决方案1】:

您可以将另一个 Collider 添加到您的玩家 GameObject 并使用 Is Trigger 选项使其成为触发器。使用此代码更改标志变量,告知玩家是否在地面上:

private bool isOnGround = false;
void OnCollisionEnter2D(Collision2D collision) {
    isOnGround = true;      
}

void OnCollisionExit2D(Collision2D collision) {
    isOnGround = false;
}

那么你只能在isOnGround为真时才允许跳转。

【讨论】:

    【解决方案2】:

    你可以制作一个叫做groundController的小游戏对象,把它放在player下面。 您正在为接地设置 bool 值,并在代码中检查您的控制器是否与地面重叠。

    在此处观看以获取更多信息:http://youtu.be/Xnyb2f6Qqzg?t=45m22s

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      • 2018-04-22
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多