[英]Unity2D shake and lagging problem while my character is moving
void FixedUpdate()
{
//Profiler.BeginSample ("MyPieceOfCode");
if (npc.GetComponent<textAnimation>().textFinished)
{
attacking();
getFire();
slashAnim();
getDamage();
}
//Profiler.EndSample ();
}
void Update()
{
cameraSystem();
if (!getDamaged && !firehit)
{
moving();
}
else
{
anim.SetBool("running", false);
}
}
void moving()
{
if ((_right.isRight || Input.GetKey(KeyCode.D)) && !isDeath)
{
walkingright=true;
spr.flipX = false;
anim.SetBool("running", true);
/*if(isGround){
transform.position = new Vector3(transform.position.x,transform.position.y-0.045f,transform.position.z);
}*/
transform.Translate(Vector3.right * playerSpeed);
if (!walking.isPlaying && isGround)
{
walking.Play();
}
if (!foodstep.isPlaying && onStone)
{
foodstep.Play();
}
}
else if ((_left.isLeft || Input.GetKey(KeyCode.A)) && !isDeath)
{
walkingright=false;
spr.flipX = true;
anim.SetBool("running", true);
/*if(isGround)
{
transform.position = new Vector3(transform.position.x,transform.position.y-0.045f,transform.position.z);
}*/
transform.Translate(Vector3.left * playerSpeed);
if (!walking.isPlaying && isGround)
{
walking.Play();
}
if (!foodstep.isPlaying && onStone)
{
foodstep.Play();
}
}
else
{
_walking=false;
anim.SetBool("running", false);
walking.Stop();
foodstep.Stop();
}
if (isGround)
{
weaponR.GetComponent<Rigidbody2D>().simulated = true;
weaponL.GetComponent<Rigidbody2D>().simulated = true;
}
else
{
weaponR.GetComponent<Rigidbody2D>().simulated = false;
weaponL.GetComponent<Rigidbody2D>().simulated = false;
}
if ((isJump.jmp || Input.GetKeyDown(KeyCode.Space)) && !isDeath && isGround)
{
rgd.AddForce(new Vector2(0, 2.750f), ForceMode2D.Impulse);
jumpAudio.Play();
isGround = false;
}
if (health.rectTransform.sizeDelta.x >= 0 && hit)
{
health.rectTransform.sizeDelta -= new Vector2(damage, 0);
hit = false;
}
}
void cameraSystem()
{
float distance = Vector2.Distance(cam.transform.position, transform.position);
Vector2 direction = transform.position - cam.transform.position;
if (distance > .8f && direction.x > 0)
{ _walking=true;
cam.transform.Translate(Vector2.right * playerSpeed);
_direction=1;
}
else if (distance > .8f && direction.x < 0)
{ _walking=true;
cam.transform.Translate(Vector2.left * playerSpeed);
_direction=-1;
}
else{
_walking=false;
}
}
这是我尝试过的另一种代码样式
void FixedUpdate()
{
//Profiler.BeginSample ("MyPieceOfCode");
cameraSystem();
if (npc.GetComponent<textAnimation>().textFinished)
{
if(!getDamaged && !firehit)
{
moving();
}
else
{
anim.SetBool("running", false);
}
attacking();
getFire();
slashAnim();
getDamage();
}
//Profiler.EndSample ();
}
void moving()
{
if ((_right.isRight || Input.GetKey(KeyCode.D)) && !isDeath)
{
walkingright=true;
spr.flipX = false;
anim.SetBool("running", true);
if(isGround){
transform.position = new Vector3(transform.position.x,transform.position.y-0.045f,transform.position.z);
}
transform.Translate(Vector3.right * Time.deltaTime * 1.75f);
if (!walking.isPlaying && isGround)
{
walking.Play();
}
if (!foodstep.isPlaying && onStone)
{
foodstep.Play();
}
}
else if ((_left.isLeft || Input.GetKey(KeyCode.A)) && !isDeath)
{
walkingright=false;
spr.flipX = true;
anim.SetBool("running", true);
if(isGround)
{
transform.position = new Vector3(transform.position.x,transform.position.y-0.045f,transform.position.z);
}
transform.Translate(Vector3.left * Time.deltaTime * 1.75f);
if (!walking.isPlaying && isGround)
{
walking.Play();
}
if (!foodstep.isPlaying && onStone)
{
foodstep.Play();
}
}
else
{
_walking=false;
anim.SetBool("running", false);
walking.Stop();
foodstep.Stop();
}
if (isGround)
{
weaponR.GetComponent<Rigidbody2D>().simulated = true;
weaponL.GetComponent<Rigidbody2D>().simulated = true;
}
else
{
weaponR.GetComponent<Rigidbody2D>().simulated = false;
weaponL.GetComponent<Rigidbody2D>().simulated = false;
}
if ((isJump.jmp || Input.GetKeyDown(KeyCode.Space)) && !isDeath && isGround)
{
rgd.AddForce(new Vector2(0, 2.750f), ForceMode2D.Impulse);
jumpAudio.Play();
isGround = false;
}
if (health.rectTransform.sizeDelta.x >= 0 && hit)
{
health.rectTransform.sizeDelta -= new Vector2(damage, 0);
hit = false;
}
}
每当我尝试移动角色时,在fps高于90的情况下,我在2D平台游戏中总会遇到烦人的晃动和滞后现象。©已经搜索了5个小时,但我找不到问题所在。 它可能会在UnityEngine内核中出错吗? 因为我注意到有很多人迷恋这个问题。 İ想像其他2D独立平台游戏一样,使动作流畅。 这是我的游戏屏幕: https : //pasteboard.co/Is3eM42.gif这是我的视频: https : //streamable.com/gi78h看山,你看到水滴了吗?
更换
rgd.AddForce(new Vector2(0, 2.750f), ForceMode2D.Impulse);
与
rgd.velocity = Vector2.up * 2.75;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.