【发布时间】:2020-09-30 12:17:35
【问题描述】:
我正在尝试让角色自动移动,但是当角色向左或向右移动时它会冻结。
视频:
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 的新手。
最后,我该如何解决我的问题?
EDIT2: 这是我的FPS。数值是正常还是异常?或者我们是否可以说“如果某些时候 FPS 低于 60,那么这个游戏的性能真的很差。FPS 应该总是等于或大于 60 才能说它的性能很好”?您认为?
【问题讨论】:
-
从视频中,角色没有冻结。它在左右移动。还有,不要在
ProcessInputs中给自己赋值moveSpeed而去掉moveDirection,你不要用。 -
请仔细看,它不会这样移动。 youtube.com/watch?v=iigQd5FpWOM 在这段视频中,角色不停地左右移动。很顺利,但我的角色动作不顺畅,仔细看,你会看到的。
-
很难在视频中看到您到底在说什么……一般来说:在处理刚体时,不要不要使用
Transform组件!您正在使用transform.Rotate(0f, 180f, 0f);这可能会破坏物理..而不是使用例如rb.MoveRotation(...)通过物理引擎 -
请激活
Deep profile并在分析器的底部找出您的滞后究竟来自哪里... -
不知道什么意思?我是新手!!我激活了,但似乎没有任何改变!
标签: c# android visual-studio unity3d