Я сделал небольшую игру с помощью обучающих программ YouTube, и у меня возникла проблема.С игрой на ПК все в порядке, я использую A и D для перемещения куба влево или вправо, чтобы избежать препятствий впереди.Блок имеет прямую скорость, чтобы двигаться вперед с заданной скоростью.
Но я хочу проверить свою игру на своем телефоне Android, поэтому мне нужно управлять объектом с помощью сенсорных кнопок или касаться левой или правой частимобильный экран.С клавишами An или D это легко, но с мобильным прикосновением ... не так.Ниже приведен код, который я сейчас использую для перемещения и изображения.
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
// This is a reference to the Rigidbody component called "rb"
public Rigidbody rb;
public float forwardForce = 2000f; // Variable that determines the forward force
public float sidewaysForce = 500f; // Variable that determines the sideways force
// We marked this as "Fixed"Update because we
// are using it to mess with physics.
void FixedUpdate ()
{
// Add a forward force
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d")) // If the player is pressing the "d" key
{
// Add a force to the right
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a")) // If the player is pressing the "a" key
{
// Add a force to the left
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (rb.position.y < -1f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
}
