- Home /
Itween, how to mouseclick movement on 3rd person camera
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.
Answer by Lorthan · Sep 07, 2014 at 07:59 PM
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.
Answer by robertbu · Sep 07, 2014 at 08:49 PM
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.
Error 1 An object reference is required for the non-static field, method, or property 'UnityEngine.Plane.Raycast(UnityEngine.Ray, out float)' C:\Users\HARRY\Documents\3D Person Camera\Assets\Scripts\Player.cs 44 17 Assembly-CSharp-vs
any misspelling robert?
That I had fixed. Still got the Plane.Raycast error. Odd one
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Problem with iTween and instantiated path 0 Answers
Problem with three lane movement 0 Answers
Unity3d 4,script nGUI btn drag convert to move where i mouse click 1 Answer
How to make an object move to another object's location in a 2d game? 2 Answers