{"id":1370,"date":"2023-02-08T19:08:08","date_gmt":"2023-02-08T18:08:08","guid":{"rendered":"https:\/\/fileidea.com\/?p=1370"},"modified":"2023-02-08T19:08:10","modified_gmt":"2023-02-08T18:08:10","slug":"unity-move-3d-object-with-input-keys","status":"publish","type":"post","link":"https:\/\/fileidea.com\/2023\/02\/08\/unity-move-3d-object-with-input-keys\/","title":{"rendered":"Unity – Move 3D object with Input Keys"},"content":{"rendered":"\n

In Unity, one of the first things that usually want to do is add some code so that I can move a 3D object with my keyboard keys. I found some ways after experimenting.<\/p>\n\n\n\n

On your object, also add the component Rigid Body<\/strong>. Then, create a new script, then drag it to your object so your object is using the script. Now, let’s change the code! My script name is ballchar, so just replace that to whatever you use.<\/p>\n\n\n\n

Move object with transform<\/h2>\n\n\n\n

One way to make the object move is to simply change the position of the object directly with transform<\/strong>. <\/p>\n\n\n

\nusing UnityEngine;\n\npublic class ballchar : MonoBehaviour\n{\n    const float speed = 0.1f;\n\n    \/\/ Update is called once per frame\n    void Update()\n    {\n        Vector3 position = transform.position;\n\n        bool keyUp = Input.GetKey(KeyCode.UpArrow);\n        bool keyDown = Input.GetKey(KeyCode.DownArrow);\n        bool keyLeft = Input.GetKey(KeyCode.LeftArrow);\n        bool keyRight = Input.GetKey(KeyCode.RightArrow);\n\n        if (keyUp)\n        {\n            position.z += speed;\n        }\n        if (keyDown)\n        {\n            position.z -= speed;\n        }\n        if (keyRight)\n        {\n            position.x += speed;\n        }\n        if (keyLeft)\n        {\n            position.x -= speed;\n        }\n\n        transform.position = position;\n    }\n} \n\n<\/pre><\/div>\n\n\n

Now you can move the objects position so it change x, y or z position in the direction of the keys you press down on the keyboard.<\/p>\n\n\n\n

Move object with velocity<\/h2>\n\n\n\n

Another way to make the object move is to change the velocity on the objects Rigid Body<\/strong>. <\/p>\n\n\n

\nusing UnityEngine;\n\npublic class ballchar : MonoBehaviour\n{\n    const float speed = 0.2f;\n\n    \/\/ Update is called once per frame\n    void Update()\n    {\n        Vector3 velocity = GetComponent<Rigidbody>().velocity;\n\n        bool keyUp = Input.GetKey(KeyCode.UpArrow);\n        bool keyDown = Input.GetKey(KeyCode.DownArrow);\n        bool keyLeft = Input.GetKey(KeyCode.LeftArrow);\n        bool keyRight = Input.GetKey(KeyCode.RightArrow);\n\n        if (keyUp)\n        {\n            velocity.z += speed;\n        }\n        if (keyDown)\n        {\n            velocity.z -= speed;\n        }\n        if (keyRight)\n        {\n            velocity.x += speed;\n        }\n        if (keyLeft)\n        {\n            velocity.x -= speed;\n        }\n\n        GetComponent<Rigidbody>().velocity = velocity;\n    }\n} \n\n<\/pre><\/div>\n\n\n

Now you can move the objects velocity so it goes in the direction of the keys you press down on the keyboard! However, if we check the documentation it says this:<\/p>\n\n\n\n

Move with CharacterController<\/h2>\n\n\n\n

The better way I would say is to add a new component to your object called “CharacterController”, here you get some things for free such as the Move<\/strong> method.<\/p>\n\n\n\n

\n

Supplies the movement of a GameObject with an attached CharacterController component.<\/p>\n\n\n\n

The\u00a0CharacterController.Move\u00a0motion moves the GameObject in the given direction. The given direction requires absolute movement delta values. A collision constrains the\u00a0Move\u00a0from taking place. The return,\u00a0CollisionFlags, indicates the direction of a collision: None, Sides, Above, and Below.\u00a0CharacterController.Move\u00a0does not use gravity.<\/p>\n<\/blockquote>\n\n\n

\nusing UnityEngine;\n\npublic class ballchar : MonoBehaviour\n{\n    private CharacterController controller;\n    private Vector3 playerVelocity;\n    private bool groundedPlayer;\n    private float speed = 2.0f;\n    private float gravityValue = -9.81f;\n\n    void Start()\n    {\n        controller = GetComponent<CharacterController>();\n    }\n    \/\/ Update is called once per frame\n    void Update()\n    {\n        groundedPlayer = controller.isGrounded;\n        if (groundedPlayer && playerVelocity.y < 0)\n        {\n            playerVelocity.y = 0f;\n        }\n\n        Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));\n        controller.Move(move * Time.deltaTime * speed);\n\n        if (move != Vector3.zero)\n        {\n            gameObject.transform.forward = move;\n        }\n\n        playerVelocity.y += gravityValue * Time.deltaTime;\n        controller.Move(playerVelocity * Time.deltaTime);\n    }\n} \n\n<\/pre><\/div>\n\n\n

Note that you might want to check under Rigidbody and Freeze Rotation to stop your objects from rotating.<\/p>\n","protected":false},"excerpt":{"rendered":"

In Unity, one of the first things that usually want to do is add some code so that I can move a 3D object with my keyboard keys. I found some ways after experimenting. On your object, also add the component Rigid Body. Then, create a new script, then drag it to your object so … <\/p>\n