Hi i'm new to unity and I am making a 3D mobile game but I am having trouble changing my player movement script. I just need to change the Input.GetKey("d") to a UI button instead. thank you so much for your help. Code (CSharp): using UnityEngine; public class PlayerMovement : MonoBehaviour { public Rigidbody rb; public float forwardForce = 2000f; public float sidewaysForce = 500f; void FixedUpdate() { // Add a forward force rb.AddForce(0, 0, forwardForce * Time.deltaTime); if ( Input.GetKey("d") ) { rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange); } if (Input.GetKey("a")) { rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange); } if (rb.position.y < -1f) { FindObjectOfType<GameManager>().EndGame(); } } }
What kind of button do you need to change it to? If a mouse button, you'd use Input.GetMouseButton instead of Input.GetKey. If a UI button, you'll need to learn more about the Unity UI than I can explain in a few sentences, but I'll put a link below to the manual section and you can find tutorials in the Learn section and on YouTube. If another kind of button, you'll have to be specific. https://docs.unity3d.com/ScriptReference/Input.GetMouseButton.html https://docs.unity3d.com/Manual/UISystem.html https://docs.unity3d.com/Manual/UIInteractionComponents.html edit: sorry, I missed the "mobile game" part. It is probably the UI button that you're looking for. You create a canvas, a few other steps, then create a button (generally all in the editor). In your script you create a method you want to call when the button is pressed. In the editor you then add the new method to the button's OnClick event. So when the button is pressed that method gets called.