I have one object, a knight, that I click on and want to move around on terrain.
private void OnMouseDown()
{
if (Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit, 10000))
{
if (unitSelected)
MoveUnit(hit);
else if (unitSelected && hit.collider == home)
GoHome(hit);
}
}
And
private void OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
MoveUnit(hit);
}
}
Both scripts work fine when clicking on the terrain.
The Problem
I want to click on different objects and send the knight towards whatever object I click on.
The knight does NOT move, though the object I click on registers a click (all objects have colliders).
Is Raycast the most appropriate way to do this? Checking another users question similar to mine, (how-to-move-one-object-to-another-objects-position) they seem to use a method similar to "Vector3.MoveTowards".
EDIT:
Moving my code from "OnMouseDown" to "Update" solves that issue. but is there a reason why OnMouseDown won't work or even how to make it work? I was trying to limit my code from going into Update as that's called every frame.