2014-10-30 92 views
1

您好,感谢您阅读本文。停止在我的Unity游戏中跳转的玩家/用户

我是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对象。

enter image description here enter image description here

如何从能够multijump停止播放。所以如果他按上箭键,他会跳起来,在他着陆之前不能再跳。 感谢您的时间和帮助

更新

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

+0

您是否试过将跳转代码移动到'FixedUpdate'而不是'Update'?这可能会产生一些影响,因为这是您检查球员是否接地的地方。 – Catwood 2014-10-30 10:57:05

回答

2

您可以添加其他撞机您的播放器游戏物体,并使其与Is Trigger选项的触发器。使用此代码来更改标志变量说服力的,如果玩家在地面上:

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

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

然后,你可以只允许跳跃时isOnGround是真实的。

2

您可以创建一个名为groundController小游戏对象,将其下播放器。 你正在设置布尔值接地和代码检查你的控制器是否与地面重叠。

看它这里获取更多信息:http://youtu.be/Xnyb2f6Qqzg?t=45m22s