C# 双跳不起作用
我已经写了一些代码来控制我的播放器。当角色点击空格键以及玩家从平台上跌落时,将播放跳跃+跳跃动画(以及衣服和头发动画) 我的双跳不起作用。我的运动员只能跳一次。有人知道为什么这不起作用吗C# 双跳不起作用,c#,unity3d,C#,Unity3d,我已经写了一些代码来控制我的播放器。当角色点击空格键以及玩家从平台上跌落时,将播放跳跃+跳跃动画(以及衣服和头发动画) 我的双跳不起作用。我的运动员只能跳一次。有人知道为什么这不起作用吗 using UnityEngine; using System.Collections; public class playerController : MonoBehaviour { private Animator anim; public Vector3 moveForward = ne
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 );
}
}
}
使用UnityEngine;
使用系统集合;
公共类玩家控制器:单行为{
私人动画师;
公共向量3向前移动=新向量3(1,0,0);
私人浮动速度=4;
私人学校停课;
私人刚体;
私人跳远;
//用于初始化
无效开始(){
rb=GetComponent();
anim=GetComponent();
双跳=真;
}
//每帧调用一次更新
无效更新(){
transform.Translate(Vector3.forward*速度*时间.deltaTime);
如果(接地){
if(Input.GetKeyDown(“空格”)){
如果(接地)
{
rb.velocity=新矢量3(10,15,0);
动画游戏(“装备|角色(动作)游戏”,-1,0f);
动画游戏(“穿衣服,跳”,1,0f);
动画游戏(“头发|电枢|跳跃”,0,0f);
isfounded=false;
}
如果(!isGrounded&&doubleJump)发生异常(!isGrounded&&doubleJump){
rb.velocity=新矢量3(10,15,0);
动画游戏(“装备|角色(动作)游戏”,-1,0f);
动画游戏(“穿衣服,跳”,1,0f);
动画游戏(“头发|电枢|跳跃”,0,0f);
双跳=假;
}
}
}
}//内更新
无效碰撞中心(碰撞其他){
如果(other.gameObject.tag==“地面”){
isGrounded=true;
双跳=真;
动画游戏(“装备|角色_跑”,-1,0f);
动画游戏(“穿衣服跑步”,1,0f);
动画游戏(“头发”|电枢|跑”,0,0f);
}
}//内酮碰撞
void OnCollisionExit(碰撞其他){
如果(other.gameObject.tag==“地面”){
isfounded=false;
}
如果(!isground){
动画游戏(“装备|角色(动作)游戏”,-1,0f);
动画游戏(“穿衣服,跳”,1,0f);
动画游戏(“头发|电枢|跳跃”,0,0f);
}
如果(接地){
动画游戏(“装备|角色_跑”,-1,0f);
}
}
}
问题是:
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语句,因为它是冗余的,实际上破坏了代码的其余部分
因此,代码应该如下所示
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;
}
}
}
嗨,我试过你的代码,现在根本不让我跳!当然不是,那个代码只是一个例子,如果你愿意,我可以在中编辑实际的代码,我是在假设中使用代码的!对不起,我只是个初学者。如果你能编辑代码,那将非常有帮助!非常感谢:)我也是初学者,所以不用担心,更新的代码现在位于答案的末尾,这将替换整个更新方法(包括声明):)