Moving objects by clicking on them
Sometimes, we want to allow the user to interact with objects through mouse pointer clicks. In this recipe, we will allow the user to move an object in a random direction by clicking on it.
Getting ready
This recipe builds upon the player-controlled 3D cube Unity project that you created at the beginning of this chapter. So, make a copy of this project, open it, and then follow the steps for this recipe. The result of following this recipe should look as follows:

How to do it...
To move objects by clicking on them, follow these steps:
- Delete the
Cube-player
GameObject. - Set the
Main Camera
position to (0, 3, -5
), and its rotation to (25, 0, 0
). - Create a C# Script class called
ClickMove
:
using UnityEngine; [RequireComponent(typeof(Rigidbody))] public class ClickMove : MonoBehaviour { public float multiplier = 500f; private Rigidbody rigidBody; private void Awake() { rigidBody = GetComponent<Rigidbody>...