Itween, how to mouseclick movement on 3rd person camera

[32129-07-09+13-39.png|32129]

So, that’s the clear image of my upcoming project, actually a task.

My next goal s to make the player (cube) go to the position where the mouse clicks on the board. Diablo Style. I’m having a little trouble trying to do the Mouse Pick since the Input.mousePosition operates in a (x,y) screen area. Tried to sum the camera position but it’s not totally accurate, and still adds Y even if I set it to 0.

Searching on iTween I’ve found - even without any explanation anywhere - the ‘movement on uneven terrain’, which is not precisely needed, since the floor if flat. Anyways, how can I do it?

The so far no-good code:

if (Input.GetMouseButtonDown(0))
{
     Vector3 point = new Vector3 (Input.mousePosition.x + cam.transform.position.x,
     0, Input.mousePosition.z + cam.transform.position.z);
     var ray = Camera.main.ScreenPointToRay(point);
     float ent = 10.0f;
     Vector3 hitPoint = ray.GetPoint(ent);

     iTween.MoveTo(target, hitPoint, 1);
}

ps: It’s a squared but not a tiled board, the player can move freely on it.

There are some ways you can do It.

I’d personally rather make it follow an empty Gameobject, the mouseclick only sets the empty Gameobject’s position and then use the MoveBy from ITween, makes it feel more organic.

That way you don’t need to use ScreenPointToRay but rather ScreenToWorldPoint.

Standard forms of converting between screen and world coordinates like Camera.ScreenToWorldPoint() will not work with an angled camera. A Raycast() is the usual solution. Use a Plane.Raycast() for an infinite or very large plane with no obstructions. A Collider.Raycast() works well with a limited plane without without obstructions. Physics.Raycast() can handle obsructions. Here is an untested rewrite of your code above to use Plane.Raycast():

if (Input.GetMouseButtonDown(0)) {
     Plane plane = new Plane(Vector3.up, transform.position);
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     float ent = 0.0f;
     if (Plane.Raycast(ray, out ent))  {
          Vector3 hitPoint = ray.GetPoint(ent);
          iTween.MoveTo(gameObject, hitPoint, 1f);
     }
}

This code goes on the block.