Trying to adjust a click to move script

I’ve got a basic click to move script I’m trying to adjust. I want to keep the player on a grid after clicking and having a little trouble getting the raycast point to adjust. I’m trying to adjust the position of where the player is set to move. This is what I have right now:

if(Input.GetMouseButtonDown(0))
        {
            if(Physics.Raycast(ray, out hit))
            {
                navAgent.SetDestination(hit.point);
            }
        }

so I want to change the hit.point to a grid based system. I have tried using each tile as a separate entity, which works for now but will create giant problems later on. That one looks something like this:

public Transform Target;

Target = hit.transform;
navAgent.SetDestination(Target.transform);

Essentially I want to change the transform into adjusting just the X and Z coordinates, that way I can use larger objects like planes instead of having to use separate tiles for the whole game. After searching around it would seem the best way to do this is when I find the X and Z coordinate of a clicked area, I adjust the coordinates to the nearest whole number. After looking around online I stumbled upon this:

Mathf.Round

which supposedly can adjust a number to the nearest integer. Is there a way I can add the Mathf.Round to the transform of a click? Or is there a way I can change the Vector3 information from a raycast by using the Mathf.Round?

Could you measure the difference between the current position & the clicked position & then use the rounding to determine which grid to move. If each grid is one unity unit you could try multiplying that number by vector3.forward (etc) to get it to move that distance you want. (I’m not sure if this will actually work, I haven’t tried it).

I actually figured it out with this

Target = hit.point;
                Target.x = Mathf.Round(Target.x);
                Target.z = Mathf.Round(Target.z);
                navAgent.SetDestination(Target);
[/CPDE]
1 Like