unity中c#跳跃代码的一些问题
这是我遇到问题的代码,事实上,当我按下空格键时,播放器只会重复跳跃的动画,甚至右箭头也不再起作用 如果你能帮忙的话,我非常欢迎你unity中c#跳跃代码的一些问题,c#,visual-studio,unity3d,2d-games,C#,Visual Studio,Unity3d,2d Games,这是我遇到问题的代码,事实上,当我按下空格键时,播放器只会重复跳跃的动画,甚至右箭头也不再起作用 如果你能帮忙的话,我非常欢迎你 using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { Rigidbody2D rb2d; public float Speed = 30f; public flo
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
Rigidbody2D rb2d;
public float Speed = 30f;
public float JumpForce = 30f;
bool Jump;
float speed;
private bool FacingRight;
// Start is called before the first frame update
void Start()
{
FacingRight = true;
rb2d = GetComponent<Rigidbody2D>();
rb2d.velocity = Vector2.zero;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.Space))
{
GetComponent<Animator>().SetBool("Jump", true);
}
if (Input.GetKey(KeyCode.RightArrow))
{
GetComponent<Animator>().SetFloat("speed", 1);
rb2d.velocity = Vector2.right * Speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
GetComponent<Animator>().SetFloat("speed", 1);
rb2d.velocity = Vector2.left * Speed * Time.deltaTime;
}
else
{
GetComponent<Animator>().SetFloat("speed", 0f);
rb2d.velocity = Vector2.zero;
}
}
private void FixedUpdate()
{
float horizontal = Input.GetAxis("Horizontal");
Flip(horizontal);
}
private void Flip (float horizontal)
{
if (horizontal > 0 && !FacingRight || horizontal < 0 && FacingRight)
{
FacingRight = !FacingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name ==" Front_Buildings")
{
GetComponent<Animator>().SetBool("isGrounded", true);
rb2d.velocity = Vector2.zero ;
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类玩家:单一行为
{
刚体2d rb2d;
公共浮子速度=30f;
公共浮子力=30f;
跳远;
浮动速度;
私人楼宇朝向权;
//在第一帧更新之前调用Start
void Start()
{
FacingRight=正确;
rb2d=GetComponent();
rb2d.velocity=Vector2.0;
}
//每帧调用一次更新
无效更新()
{
if(Input.GetKey(KeyCode.Space))
{
GetComponent().SetBool(“跳转”,true);
}
if(Input.GetKey(KeyCode.RightArrow))
{
GetComponent().SetFloat(“速度”,1);
rb2d.velocity=Vector2.right*速度*时间.deltaTime;
}
if(Input.GetKey(KeyCode.LeftArrow))
{
GetComponent().SetFloat(“速度”,1);
rb2d.velocity=Vector2.left*速度*时间.deltaTime;
}
其他的
{
GetComponent().SetFloat(“速度”,0f);
rb2d.velocity=Vector2.0;
}
}
私有void FixedUpdate()
{
float horizontal=Input.GetAxis(“水平”);
翻转(水平);
}
专用空心翻转(水平浮动)
{
如果(水平>0&&!朝向右侧| |水平<0&&朝向右侧)
{
FacingRight=!FacingRight;
Vector3 theScale=transform.localScale;
比例x*=-1;
transform.localScale=比例;
}
}
空心OnCollisionInter2D(碰撞2D列)
{
如果(col.gameObject.name==“前面的建筑”)
{
GetComponent().SetBool(“IsGround”,true);
rb2d.velocity=Vector2.0;
}
}
}
看起来您在此处将animator跳跃属性设置为“true”
GetComponent().SetBool(“Jump”,true)代码>
你有没有把这个设置回false?您只希望它为真一次,为了开始动画,那么应该将它设置回false,这样动画就不会无休止地重复。不要忘记将跳转设置回false
GetComponent().SetBool(“跳转”,false)代码>
当(未)按下左箭头时,您还有一个else
。因此,右箭头(不是左箭头)将速度和速度设置为0。猜测您缺少一个“GetComponent().SetBool”(“Jump”,false);”