我已经写了一些代码来控制我的播放器。跳跃+跳跃动画(连同衣服和头发动画)在角色击中空格键和玩家从平台上掉落时播放。
我的双跳不起作用。我的玩家只能跳一次。谁能明白为什么这不起作用?
using UnityEngine;
using System.Collections;
public class playerController : MonoBehaviour {
private Animator anim;
public Vector3 moveForward = new Vector3(1,0,0);
private float speed = 4;
private bool isGrounded;
private Rigidbody rb;
private bool doubleJump;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
anim = GetComponent<Animator>();
doubleJump = true;
}
// Update is called once per frame
void Update () {
transform.Translate(Vector3.forward * speed * Time.deltaTime);
if(isGrounded) {
if(Input.GetKeyDown("space")) {
if(isGrounded)
{
rb.velocity = new Vector3(10, 15, 0);
anim.Play("rig|Character_ActionsJUMP", -1, 0f);
anim.Play("Dress_Armature |Jump", 1, 0f);
anim.Play("Hair_Armature|Jump", 0, 0f);
isGrounded = false;
}
else if (!isGrounded && doubleJump) {
rb.velocity = new Vector3(10, 15, 0);
anim.Play("rig|Character_ActionsJUMP", -1, 0f);
anim.Play("Dress_Armature |Jump", 1, 0f);
anim.Play("Hair_Armature|Jump", 0, 0f);
doubleJump = false;
}
}
}
} //endOFUpdate
void OnCollisionEnter(Collision other) {
if(other.gameObject.tag == "Ground") {
isGrounded = true;
doubleJump = true;
anim.Play("rig|Character_Run", -1, 0f );
anim.Play("Dress_run", 1, 0f );
anim.Play("Hair_Armature|run", 0, 0f );
}
}//endOfOnCollion
void OnCollisionExit(Collision other) {
if(other.gameObject.tag == "Ground") {
isGrounded = false;
}
if(!isGrounded) {
anim.Play("rig|Character_ActionsJUMP", -1, 0f);
anim.Play("Dress_Armature |Jump", 1, 0f);
anim.Play("Hair_Armature|Jump", 0, 0f);
}
if(isGrounded) {
anim.Play("rig|Character_Run", -1, 0f );
}
}
}
答案 0 :(得分:1)
问题在于:
if(isGrounded) {
if(Input.GetKeyDown("space")) {
if(isGrounded)
{
rb.velocity = new Vector3(10, 15, 0);
anim.Play("rig|Character_ActionsJUMP", -1, 0f);
anim.Play("Dress_Armature |Jump", 1, 0f);
anim.Play("Hair_Armature|Jump", 0, 0f);
isGrounded = false;
}
else if (!isGrounded && doubleJump) {
rb.velocity = new Vector3(10, 15, 0);
anim.Play("rig|Character_ActionsJUMP", -1, 0f);
anim.Play("Dress_Armature |Jump", 1, 0f);
anim.Play("Hair_Armature|Jump", 0, 0f);
doubleJump = false;
}
}
}
在此代码块中,除非它们接地,否则不要输入空格键的检查,如果我们忽略第二次检查它们是否已接地,则代码将变为此
if(isGrounded) {
if(Input.GetKeyDown("space")) {
if (!isGrounded && doubleJump) {
rb.velocity = new Vector3(10, 15, 0);
anim.Play("rig|Character_ActionsJUMP", -1, 0f);
anim.Play("Dress_Armature |Jump", 1, 0f);
anim.Play("Hair_Armature|Jump", 0, 0f);
doubleJump = false;
}
}
}
从这里可以更容易地看到问题,最后一个如果永远无法实现,因为基础必须是真的和假
所以解决方法是删除原来的if语句,因为它是reduntant,实际上会破坏其余的代码!
因此,代码应该如下
void Update () {
transform.Translate(Vector3.forward * speed * Time.deltaTime);
if(Input.GetKeyDown("space")) {
if(isGrounded)
{
rb.velocity = new Vector3(10, 15, 0);
anim.Play("rig|Character_ActionsJUMP", -1, 0f);
anim.Play("Dress_Armature |Jump", 1, 0f);
anim.Play("Hair_Armature|Jump", 0, 0f);
isGrounded = false;
}
else if (!isGrounded && doubleJump) {
rb.velocity = new Vector3(10, 15, 0);
anim.Play("rig|Character_ActionsJUMP", -1, 0f);
anim.Play("Dress_Armature |Jump", 1, 0f);
anim.Play("Hair_Armature|Jump", 0, 0f);
doubleJump = false;
}
}
}