Click object to move player/object

Hello everyone.
I recently started with unity, and have been experimenting with different mechanics for potentials games. I however, can not figure out a few key parts of the thing i’m trying to do:

Problem 1

When I click on an object, I want that object selected, so it can be moved (e.g. with the arrow keys), and when I click on another object, the first object should be unselected, so it no longer moves.

Problem 2

When I click on an object in the game (e.g. an arrow pointing in a specific direction) I want the player to move in that same direction.


Both of these mechanics are becoming quite the hassle for me, but I’m sure there is an easy solution for this I haven’t thought of. I would appreciate any help I could get.

Thanks in advance.

Hi @KjipGamer,

For Problem 1:

You can attach to the selectable GameObject, a MonoBehaviour that implements the message OnMouseDown. This will allow you to get notified when the left mouse button is pressed over any of the GameObject colliders. When the OnMouseDown message occurs, you can notify a controller script (e.g. “SelectionController”) that a selectable GameObject was clicked. The controller will then be responsible to select the clicked GameObject and deselect any previous selected one.

For Problem 2:

For this one, first you will need the get the position where you’ve pressed the mouse. Input.mousePosition provides the mouse position in pixel coordinates but you will need to get the mouse position in world coordinates. The way to do this depends if you are working in 2D or 3D.

2D:

The 2D solution is quit simpler. You just need a reference to your camera component to call the method Camera.ScreenToWorldPoint and pass as parameter the Input.mousePosition (e.g camera.ScreenToWorldPoint(Input.mousePosition)).

3D:

If you are working in 3D, you will need fire a Ray towards what was clicked and then if the ray hit some GameObject, you just get position of that GameObject. To create a ray from the screen towards the point that was clicked, use this method: Camera.ScreenPointToRay and pass the Input.mousePosition as parameter. Then you’ll have to cast the ray using Physics.Raycast or Physics.RaycastAll.

Exemple code:

Vector3 mousePressedPosition;
RaycastHit rayHit;

if (Input.GetMouseButtonDown(0))
{
    Ray ray = camera.ScreenPointToRay(Input.mousePosition);

    if(Physics.Raycast(ray, out rayHit))
    {
        mousePressedPosition = rayHit.collider.transform.position;
    }
}

Now that you have the mouse pressed position in world coordinates. you just need to calculate the direction from the selected GameObject to the point where you’ve pressed the mouse button
(Calculate a direction is quit simple, just check this link: directions) and then apply velocity to your GameObject on that direction.

If you are using a Rigidbody you will have something like this:

Vector3 direction = (mousePressedPosition - transform.position).normalized;
rigidbody.velocity = direction * myVelocity;

OBS.: Remeber that the OnMouseDown message and all other mouse messages only works for the left mouse button. If you need to use the other mouse buttons, you’ll have to implement your own mouse event system using Raycast. if that is the case, check those links:

2D: Physics2D.GetRayIntersection, Physics2D.GetRayIntersectionAll

3D: Physics.Raycast, Physics.RaycastAll