android 移动的设备上Unity游戏中的触摸响应延迟

n8ghc7c1  于 2024-01-04  发布在  Android
关注(0)|答案(1)|浏览(233)

我在移动的设备上的Unity游戏中遇到了触摸响应滞后的问题,特别是在发射刀时。在电脑上一切都很顺利,但是,在将游戏传输到手机后,我注意到延迟。
下面是处理刀镜头的代码片段:

  1. using UnityEngine;
  2. public class KnifeScript : MonoBehaviour
  3. {
  4. [SerializeField]
  5. private Vector2 throwForce;
  6. //knife shouldn't be controlled by the player when it's inactive
  7. //(i.e. it already hit the log / another knife)
  8. private bool isActive = true;
  9. //for controlling physics
  10. private Rigidbody2D rb;
  11. //the collider attached to Knife
  12. private PolygonCollider2D knifeCollider;
  13. private void Awake()
  14. {
  15. rb = GetComponent<Rigidbody2D>();
  16. knifeCollider = GetComponent<PolygonCollider2D>();
  17. }
  18. [SerializeField] private AudioSource swist;
  19. [SerializeField] private AudioSource Gdzwiek;
  20. void Update()
  21. {
  22. //this method of detecting input also works for touch
  23. if (Input.GetMouseButtonDown(0) && isActive)
  24. {
  25. rb.AddForce(throwForce, ForceMode2D.Impulse);
  26. GameController.Instance.GameUI.DecrementDisplayedKnifeCount();
  27. rb.gravityScale = 1;
  28. //once the knife isn't stationary, we can apply gravity (it will not automatically fall down)
  29. //Decrement number of available knives
  30. swist.Play();
  31. }
  32. }
  33. [SerializeField] private AudioSource Gloss;
  34. private void OnCollisionEnter2D(Collision2D collision)
  35. {
  36. //we don't even want to detect collisions when the knife isn't active
  37. if (!isActive)
  38. return;
  39. //if the knife happens to be active (1st collision), deactivate it
  40. isActive = false;
  41. //collision with a log
  42. if (collision.collider.tag == "Log")
  43. {
  44. GetComponent<ParticleSystem>().Play();
  45. Gdzwiek.Play();
  46. //stop the knife
  47. rb.velocity = new Vector2(0, 0);
  48. //this will automatically inherit rotation of the new parent (log)
  49. rb.bodyType = RigidbodyType2D.Kinematic;
  50. transform.SetParent(collision.collider.transform);
  51. //move the collider away from the blade which is stuck in the log
  52. knifeCollider.offset = new Vector2(knifeCollider.offset.x, -0.1f);
  53. //Spawn another knife
  54. GameController.Instance.OnSuccessfulKnifeHit();
  55. }
  56. //collision with another knife
  57. else if (collision.collider.tag == "Knife")
  58. {
  59. //start rapidly moving downwards
  60. rb.velocity = new Vector2(rb.velocity.x, -2);
  61. //Game Over
  62. GameController.Instance.StartGameOverSequence(false);
  63. Gloss.Play();
  64. }
  65. }
  66. }

字符串
我使用的是Unity的最新版本,我正在Android手机上进行测试
有谁能帮我找出延迟的原因吗?我是否应该在Rigidbody 2D设置或其他组件中进行调整?
提前感谢您的任何帮助!

px9o7tmv

px9o7tmv1#

我们在移动的设备上进行触摸操作时遇到的错误可能有很多原因,其中包括;

  • 由于移动的设备的性能较低,您的游戏的帧速率可能较低。请尝试提高游戏的性能。
  • 触摸灵敏度因设备而异,我的建议是在另一台移动终端上测试。
  • 当你重载一个特定的函数时,它可能会延迟响应。我建议你尝试FixexUpdate,它独立于Update工作。

这些只是我目前能猜到的一部分,可能还有其他原因。
我有一些额外的建议。首先,尝试使用Input.GetTouch而不是Input.GetMouseButton。因为Unity是专门为移动的设备开发的。

  1. void FixedUpdate()
  2. {
  3. if (isActive && Input.touchCount > 0 && TouchPhase.Began == Input.GetTouch(0).phase)
  4. {
  5. //Scripts...
  6. }

字符串
如果可能,请根据其局部坐标移动刀片。

  1. void FixedUpdate()
  2. {
  3. if (isActive && Input.touchCount > 0 && TouchPhase.Began == Input.GetTouch(0).phase)
  4. {
  5. Vector2 throwForceGlobal = transform.TransformDirection(throwForce);
  6. rb.AddForce(throwForceGlobal, ForceMode2D.Impulse);
  7. GameController.Instance.GameUI.DecrementDisplayedKnifeCount();
  8. rb.gravityScale = 1;
  9. swist.Play();
  10. isActive = false;
  11. }
  12. }

展开查看全部

相关问题