I'm currently trying to implement a move-to-drag behaviour in a 2d grid based game. I want to update the camera such that if follows the "head" of the trail being drawn - In this case, the nodes it is made up of some nodes returned from A* pathfinding utility implementation.
AStarPathfind pather = new AStarPathfindAroundWalls( LayerMask.GetMask("Walls"), Camera.main.ScreenToWorldPoint(Input.mousePosition) ); List<Vector2> trailPoints = pather.FindPath(new Vector2(this.transform.position.x, this.transform.position.y)); Camera.main.transform.position = new Vector3( trailPoints[ArrowTrailPoints.Count - 1].x, trailPoints[ArrowTrailPoints.Count - 1].y, Camera.main.transform.position.z );
When I run this code, the camera sucessfully does indeed snap to the head of the path, but unfortunately, the mouse remains where it originally was, and since this code is ran during an update loop, it will simply keep adding to the path until it can no longer do so. Essentially I would like it to go to the next tile, update the camera, reset the mouse position and then await where the next part of the path should be drawn (of course, this is not possible, since I cannot modify the mouse position directly).
I've tried storing the trail points as a property in the object, and then removing the last known position from the calculation, like so
Camera.main.ScreenToWorldPoint(Input.mousePosition) - lastTrailPoints[lastTrailPoints.Count - 1]
so that the input position would be relative to the amount offset by the camera... but with no success, so I'm starting to think I'm way off using this method. If I do something like
Camera.main.ScreenToWorldPoint(Input.mousePosition) - (MovementNodes[MovementNodes.Count - 1] - this.transform.position)
Then things work almost as expected, but the mouse will still move off of the screen, and I want to keep it centered over the actual head of the trail (where the camera is).
(black cross = current mouse position, green cross = where I want the mouse pointer to be located, camera is centered over the arrow head/green cross)
Has anyone implemented something similar before and know of a solution?