我正在尝试让角色自动移动,但当角色向左或向右时会冻结。
视频链接:
https://www.youtube.com/watch?v=Ah4cYdXN8Y8
角色移动脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public float moveSpeed;
public float moveDirection;
public float jumpForce;
public AudioSource audioSource;
private Rigidbody2D rb;
private bool facingRight = true;
private bool isJumping = false;
private string direction = "right";
// Start is called before the first frame update
void Start()
{
// Screen.SetResolution(640, 480, true);
}
// Update is called once per frame
void Update()
{
ProcessInputs();
// FlipCharacter();
if(direction == "right"){
MoveRight();
}
if(direction == "left"){
MoveLeft();
}
}
private void FixedUpdate()
{
// Move();
}
private void OnCollisionEnter2D(Collision2D c)
{
if(c.gameObject.tag == "Rightwall"){
Debug.Log("wall");
rb.velocity = new Vector2(-1 * moveDirection * moveSpeed, rb.velocity.y);
direction = "left";
}
if (c.gameObject.tag == "Leftwall")
{
direction = "right";
}
}
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
private void MoveRight()
{
rb.velocity = new Vector2(moveDirection * moveSpeed, rb.velocity.y);
if(isJumping){
Debug.Log("yep");
rb.AddForce(new Vector2(0f, jumpForce),ForceMode2D.Impulse);
audioSource.Play();
}
isJumping = false;
}
private void MoveLeft()
{
rb.velocity = new Vector2(-1* moveDirection * moveSpeed, rb.velocity.y);
if (isJumping)
{
Debug.Log("yep");
rb.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Impulse);
audioSource.Play();
}
isJumping = false;
}
private void ProcessInputs()
{
moveSpeed = moveSpeed;
if(Input.GetButtonDown("Jump")){
isJumping = true;
// Debug.Log("jump");
}
}
private void FlipCharacter(){
facingRight = !facingRight;
transform.Rotate(0f, 180f, 0f);
}
}
游戏中的角色被放大了6倍。
因此,我该如何解决我的问题?我错在哪里了?
我很努力地解决这个问题,但是我无法解决它。
我该如何解决这个延迟问题?我是Unity的新手。
编辑:我的意思是角色不连续移动。它是以离散的方式移动。我的目的是使移动更加平滑。在计算机编程中,我知道无法使运动%100平滑,但用户不应该注意到“离散”运动。移动应该看起来更加逼真。
我不知道真正的问题所在。这可能是FPS问题(Unity问题和显示问题),或者我在脚本中做错了什么。我是Unity的新手。
最后,我该如何解决我的问题?
编辑2:
这是我的FPS。这些值正常还是异常?或者我们能否说,“如果FPS在某些时刻低于60,那么这个游戏的性能真的很差。FPS应始终等于或大于60,以说明其性能良好。”你怎么看?
ProcessInputs
中不要将moveSpeed
赋值给自身,并且删除moveDirection
,因为你没有使用它。 - jiveturkeyTransform
组件!你正在使用transform.Rotate(0f, 180f, 0f);
,这可能会破坏物理效果..而应该使用例如rb.MoveRotation(...)
通过物理引擎进行旋转。 - derHugo