I’m creating a “tennis style” game. What I would like is for mouse (touch) input within the orange rectangle to command my bottom white cylinder to go to that point, snapping to the nearest grid intersection. The cylinder will never be moving backwards or forward.
I have found tutorials for settings waypoints using the navmesh and navmeshagent, but I do not think it’s necessary and is probably expensive in my case (planning for mobile).
Can anybody give me a simple example of where to start with this? Much appreciated.
You can raycast from the Camera into the world and see if it hits the orange rect.
If it does, then drop the Raycast impact point to your desired line of motion, and move your player there, either instantly or else at a specific move rate.
Thanks Kurt, the part I’m mostly unsure about is how to snap this input/movement command to the nearest grid intersections, or the center of a grid cell.
So the general case to snap something is to know what the grid cell size is, and divide your input position by that amount, take the integer of that amount, the multiply it again by the grid size.
For instance, doing it only on the X axis:
float MyGridSize = 1.5f;
Vector3 InputPosition = ... (get this from your input raycast) ..
Vector3 FinalPosition = InputPosition; // copy it over
// quantize it to grid positions
FinalPosition.x = ((int)(FinalPosition.x / MyGridSize)) * MyGridSize;
// and if you want to move it to the center of each grid interval:
FinalPosition.x += MyGridSize / 2;