Snap to node?

How would i go about snapping my raycasting mouse (or the object attached to my ray hit position) to different nodes that i have around the terrain?

The raycasting and what not is already in place, I just dont know where to look for snapping my mouse to a node…

Thanks

-Aaron

If the nodes are irregularly spaced, you can search for the node with the smallest (squared) distance to the point in question, and then snap to that node. If the nodes are regularly spaced, you can compute the closest node to the point in question using some simple arithmetic.

I guess what I dont get is what type of syntax or what reserved unity words/functions would i be looking for to snap my raycasting mouse to those nodes.

Thanks for the help :slight_smile:
-Aaron

I would use Camera.WorldToScreenPoint on the coords of all the nodes in the scene, then find the node with the closest coords to the Input.mousePosition. As for the actual snapping, that is a little more complicated. Check out http://answers.unity3d.com/questions/1913/set-cursor-position for some ideas.

Technically i guess i could keep the cursor fluid but just snap the object attached to the cursor around. Before i was using an imaginary grid through (myObject is attached to hit.point of my raycast):

myObject.transform.position.x = Mathf.Round(myObject.transform.position.x / gridSpacing) * gridSpacing;
myObject.transform.position.z = Mathf.Round(myObject.transform.position.z / gridSpacing) * gridSpacing;

Now, i just want this object to snap to predefined nodes. I guess thats the real question lol. Would i use something similar to the above script but somehow plug in node positions?

and thanks for the reply :slight_smile:

-Aaron

Again, it’s like Jesse said, if they are arranged in a grid pattern or some other mathmatically defined pattern, u can use math to calculate the snap position (like in your example). But if they are arbitrarily placed, you will have to gather them into an array (GameObject.FindGameObjectsWithTag(“node”)) and iterate over them, converting their coords to a screen position. Keep a reference to the node with the closest (sqrd) distance from the cursor’s screen position, then place the node marker or fake cursor icon and the closest node’s world space (or screen space, depending on what effect you want) position.