【发布时间】:2019-08-10 12:54:41
【问题描述】:
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;
}
}
每当我尝试移动我的角色时,我发现我的 2D 平台游戏中总是出现令人讨厌的抖动和滞后,而 fps 高于 90。 我已经搜索了 5 个小时,但我不知道我的问题出在哪里。 它可能在 UnityEngine 的内核中出现错误吗?因为我注意到有很多人对这个问题很感兴趣。 我想要平滑移动,就像在其他 2D 独立平台游戏中一样。 这是我的游戏画面:https://pasteboard.co/Is3eM42.gif 这是我的视频:https://streamable.com/gi78h 看山,有水滴吗?
【问题讨论】:
-
它没有解决我的问题
标签: c# unity3d game-engine game-physics