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: Player control of a 3D GameObject. So make a copy of that project, open it, and then follow the steps for this recipe.
The result of this recipe should look as follows:
Figure 11.12: A pyramid of cubes falling down after being clicked on
How to do it...
To move objects by clicking on them, follow these steps:
- Delete the
Cube-player
GameObject. - Set the position of
Main Camera
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...