hi! Im trying to code a script to move a sphere. I tried out this script, unity doesn't seem to have any problem with it, however, whenever I run the game the sphere doesn't move in any direction. could someone please help me out here? thank you! here's the script using UnityEngine; public class playermovement2 : MonoBehaviour { public Rigidbody rb; void start () { if (Input.GetKey("w")) { transform.position = new Vector3(0, 1, 0); } if (Input.GetKey("s")) { transform.position = new Vector3(0, -1, 0); } if (Input.GetKey("d")) { transform.position = new Vector3(1, 0, 0); } if (Input.GetKey("a")) { transform.position = new Vector3(-1, 0, 0); } } }
1. when you post code, please use code tags. 2. Your code is in the start method, so it only runs once. So unless you hit one of the movement keys at precisely the right time it will never see it. 3. Move your code into the update method.
Code (CSharp): public class MoveMe : MonoBehaviour { public Rigidbody rb; void start() { } void Update() { if (Input.GetKey("w")) { transform.position = new Vector3(0, 1, 0); } if (Input.GetKey("s")) { transform.position = new Vector3(0, -1, 0); } if (Input.GetKey("d")) { transform.position = new Vector3(1, 0, 0); } if (Input.GetKey("a")) { transform.position = new Vector3(-1, 0, 0); } } }
thank you so much, it actually works now! Apologies for not using the code tags, I'm new so I didn't realize that I had to do that, thanks again!