我在移动的设备上的Unity游戏中遇到了触摸响应滞后的问题,特别是在发射刀时。在电脑上一切都很顺利,但是,在将游戏传输到手机后,我注意到延迟。
下面是处理刀镜头的代码片段:
using UnityEngine;
public class KnifeScript : MonoBehaviour
{
[SerializeField]
private Vector2 throwForce;
//knife shouldn't be controlled by the player when it's inactive
//(i.e. it already hit the log / another knife)
private bool isActive = true;
//for controlling physics
private Rigidbody2D rb;
//the collider attached to Knife
private PolygonCollider2D knifeCollider;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
knifeCollider = GetComponent<PolygonCollider2D>();
}
[SerializeField] private AudioSource swist;
[SerializeField] private AudioSource Gdzwiek;
void Update()
{
//this method of detecting input also works for touch
if (Input.GetMouseButtonDown(0) && isActive)
{
rb.AddForce(throwForce, ForceMode2D.Impulse);
GameController.Instance.GameUI.DecrementDisplayedKnifeCount();
rb.gravityScale = 1;
//once the knife isn't stationary, we can apply gravity (it will not automatically fall down)
//Decrement number of available knives
swist.Play();
}
}
[SerializeField] private AudioSource Gloss;
private void OnCollisionEnter2D(Collision2D collision)
{
//we don't even want to detect collisions when the knife isn't active
if (!isActive)
return;
//if the knife happens to be active (1st collision), deactivate it
isActive = false;
//collision with a log
if (collision.collider.tag == "Log")
{
GetComponent<ParticleSystem>().Play();
Gdzwiek.Play();
//stop the knife
rb.velocity = new Vector2(0, 0);
//this will automatically inherit rotation of the new parent (log)
rb.bodyType = RigidbodyType2D.Kinematic;
transform.SetParent(collision.collider.transform);
//move the collider away from the blade which is stuck in the log
knifeCollider.offset = new Vector2(knifeCollider.offset.x, -0.1f);
//Spawn another knife
GameController.Instance.OnSuccessfulKnifeHit();
}
//collision with another knife
else if (collision.collider.tag == "Knife")
{
//start rapidly moving downwards
rb.velocity = new Vector2(rb.velocity.x, -2);
//Game Over
GameController.Instance.StartGameOverSequence(false);
Gloss.Play();
}
}
}
字符串
我使用的是Unity的最新版本,我正在Android手机上进行测试
有谁能帮我找出延迟的原因吗?我是否应该在Rigidbody 2D设置或其他组件中进行调整?
提前感谢您的任何帮助!
1条答案
按热度按时间px9o7tmv1#
我们在移动的设备上进行触摸操作时遇到的错误可能有很多原因,其中包括;
FixexUpdate
,它独立于Update
工作。这些只是我目前能猜到的一部分,可能还有其他原因。
我有一些额外的建议。首先,尝试使用
Input.GetTouch
而不是Input.GetMouseButton
。因为Unity是专门为移动的设备开发的。字符串
如果可能,请根据其局部坐标移动刀片。
型