C# Unity2D-不均匀跳跃,跳得太高
我正在使用C#在Unity 5中为学校构建一个2D平台游戏。有时候,当我的角色去跳的时候,跳得非常高。它不会以任何规律或任何特定的位置发生。起初我使用AddForce,然后我试图改变rigidbody2D上的Y速度,但没有解决问题。我将跳转代码从Update移动到FixedUpdate,但这只会降低跳转的响应速度。我使用的是GetKeyDown,所以在键按下的整个过程中不会施加力。我使用了Debug.Log并运行了几个测试,以确保跳转代码不会在每次按键时执行多次。我读到它可能与如何检测接地有关,但我可以在跳转代码中的几个地方将接地设置为false,但它仍然没有帮助。我已经将用于检测地面的代码从FixedUpdate移动到Update,但没有用。下面是我的跳转代码和我用来探测地面的代码C# Unity2D-不均匀跳跃,跳得太高,c#,unity3d-2dtools,C#,Unity3d 2dtools,我正在使用C#在Unity 5中为学校构建一个2D平台游戏。有时候,当我的角色去跳的时候,跳得非常高。它不会以任何规律或任何特定的位置发生。起初我使用AddForce,然后我试图改变rigidbody2D上的Y速度,但没有解决问题。我将跳转代码从Update移动到FixedUpdate,但这只会降低跳转的响应速度。我使用的是GetKeyDown,所以在键按下的整个过程中不会施加力。我使用了Debug.Log并运行了几个测试,以确保跳转代码不会在每次按键时执行多次。我读到它可能与如何检测接地有关,
in LateUpdate() grounded = Physics2D.OverlapCircle(GroundCheck.position, groundRadius, whatIsGround); //Use proper variables to check for ground and set grounded bool accordingly
in Update()//JUMPING-----------------------------------------------------------------------
if (grounded && //If the player is on the ground,
Input.GetKeyDown(KeyCode.Space) && //and the jump button is pressed,
RanaAnim.GetBool("Blocking") == false && //and Rana is not blocking,
!attacking) //or performing an attack
//&& GameManager.InputAllowed) //and the game is not paused...
{
if (GameManager.characterSwitch) //If Rana is the active character...
{
gameObject.GetComponent<AudioSource>().clip = RanaJump; //Prime Rana's jump audio clip
gameObject.GetComponent<AudioSource>().Play(); //Play Rana's jump audio clip
}
else //If Haroun is the active character...
{
gameObject.GetComponent<AudioSource>().clip = HarounJump; //Prime Haroun's jump audio clip
gameObject.GetComponent<AudioSource>().Play(); //Play Haroun's jump audio clip
}
HarounAnim.SetBool("Ground", false); //Communicate jumping to Haroun's Animator
RanaAnim.SetBool("Ground", false); //Communicate jumping to Rana's Animator
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, 0); //Zero out vertical velocity
GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, jumpForce)); //Assign jump strength via AddForce on Y axis (Jumpforce Settings: Haroun|1670 Rana|4000)
//GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpForce); //Jump Strength via velocity on Y axis (Jumpforce Settings: Haroun|25 Rana|75)
}
LateUpdate()中的接地=Physics2D.OverlappCircle(GroundCheck.position、groundRadius、whatIsGround)//使用适当的变量检查接地,并相应地设置接地bool
在Update()//中-----------------------------------------------------------------------
如果(地面&&//如果球员在地面上,
Input.GetKeyDown(KeyCode.Space)&&&//然后按下跳转按钮,
RanaAnim.GetBool(“阻塞”)==false&&//,并且Rana没有阻塞,
!攻击)//或执行攻击
//&&GameManager.InputAllowed)//并且游戏未暂停。。。
{
if(GameManager.characterSwitch)//如果Rana是活动角色。。。
{
gameObject.GetComponent().clip=RanaJump;//初始化Rana的跳转音频剪辑
gameObject.GetComponent().Play();//播放Rana的跳转音频剪辑
}
否则//如果Haroun是活跃角色。。。
{
gameObject.GetComponent().clip=HarounJump;//初始化Haroun的跳转音频剪辑
gameObject.GetComponent().Play();//播放哈伦的跳转音频剪辑
}
SetBool(“地面”,错误);//与哈伦的动画师交流跳跃
RanaAnim.SetBool(“地面”,错误);//与Rana的动画师交流跳跃
GetComponent().velocity=新矢量2(GetComponent().velocity.x,0);//将垂直速度归零
GetComponent().AddForce(新矢量2(0f,jumpForce));//通过Y轴上的AddForce分配跳跃强度(跳跃力设置:Haroun | 1670 Rana | 4000)
//GetComponent().velocity=new Vector2(GetComponent().velocity.x,jumpForce);//通过Y轴上的速度的跳跃强度(jumpForce设置:Haroun | 25 Rana | 75)
}
还有一点背景:玩家有两个可以随时互换的角色。一个名为Haroun,另一个名为Rana,因此有多个字符引用。Haroun和Rana是各自独立的对象,具有各自的sprite渲染器和碰撞器,可通过布尔值关闭和打开。Rana和Haroun对象都是此脚本所附加到的角色对象的子对象。可能是某个代码更改了jumpForce的值,或者角色与地面碰撞的事实导致了一些奇怪的错误。如果是后者,请确保对撞机的大小/形状没有任何变化,如果所有其他操作都失败,请在跳跃时将玩家稍微向上传送。谢谢。将玩家稍微向上传送似乎奏效了。这很难说,因为我不能让这个小故障发生在命令上,但我在测试了一段时间后还没有看到它,我本希望能以我以前看到的频率看到它。只向上传送角色。5个单位不会产生视觉效果,但似乎可以解决问题。这是用来修复它的代码:gameObject.transform.position=newvector2(gameObject.transform.position.x,gameObject.transform.position.y+.5f);你的球员有多高?不要忘记,稍后,这个解决方案可能会导致问题,当/如果你将使隧道正是球员的高度。