【发布时间】: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