How to handle snapping to an "anchor" point?
I'm creating with a similar style to tower defense and my only issue so far is how to handle the game board. I want my shop for the items to be drag and drop on to the game board. My only issue, how would I go about setting this up? My one idea was to place a bunch of empty game objects and when they drag the object get the current location and if it is within 'X' of an anchor point, then set the position to that anchor point so it seems to "snap" to the anchor. Now this would be a fairly simple way to go about doing it, but I have a game board of 9x13 which would be 117 tiles I'd need to place an anchor point on which would take some time and how would I go about calling it in code? Would I just need to set them up in an array or what would I need to do to see which anchor point is closest and if the item is 'X' away from the nearest then snap to it?
Answer by Namey5 · Oct 10, 2016 at 07:38 AM
I personally would do it as you say, most likely using raycasting to handle the movement of the objects. Something like the following;
using UnityEngine;
using System.Collections;
public class ObjectPlacement : MonoBehaviour
{
public GameObject placementObject; //Could also have this as an array to set up multiple objects
public Vector3 visualOffset; //The offset for the positioning of the visual
public Transform[] anchors; //You could set this automatically in scripting, or do it manually in the inspector
public float snapDistance; //The minimum distance to snap the object
private GameObject visual; //A visual representation of the position of the object
private bool isPlaced; //Checking if the object has been placed
private Transform currentAnchor;
void Start ()
{
GameObject[] g = GameObject.FindGameObjectsWithTag ("Anchor");
anchors = new Transform[g.Length];
for (int i = 0; i < anchors.Length; i++) {
anchors[i] = g[i].transform;
}
}
void Update ()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); //Create a ray at the mouse position
if (Physics.Raycast (ray, out hit)) //If the ray hits something
{
if (!visual)
visual = (GameObject) Instantiate (placementObject, hit.point + visualOffset, Quaternion.identity); //Create an instance of the visual if there isn't one already
else if (!isPlaced) //If the object hasn't already been placed
{
foreach (Transform anchor in anchors) //For every anchor point
{
if (!currentAnchor)
{
visual.transform.position = hit.point + visualOffset;
if (Vector3.Distance (hit.point, anchor.position) < snapDistance)
{
currentAnchor = anchor;
}
else
{
currentAnchor = null;
}
}
else
{
if (Vector3.Distance (hit.point, currentAnchor.position) < snapDistance) //If the visual is within snapping distance
{
visual.transform.position = currentAnchor.position + visualOffset; //Snap the visual to that anchor
}
else
{
visual.transform.position = hit.point + visualOffset; //Else have the visual at the mouse position + the pre-defined offset
currentAnchor = null;
}
}
}
}
if (Input.GetKeyDown (KeyCode.Mouse0)) //If the mouse ray hits and we click the left mouse button
{
Instantiate (placementObject, visual.transform.position, Quaternion.identity); //Create the object
isPlaced = true; //Prevent further placing for the moment (you can enable this yourself elsewhere)
}
}
}
}
Now this may or may not be what you are looking for, as it will depend on how the rest of your project is set up, but you could use this and add as many anchors as you want. Just keep in mind the more anchors you have, the more times it's going to execute that main block of code in the centre.
Thanks for the response! Sorry for this being a super late reply, haven't had a chance to use this from school and work, but I finally was able to. $$anonymous$$y only issue is, how would I go about putting all of the anchor points in the array programmatically? I have a 9*13 board, so 116 anchors, excluding the one for the middle, as I have something occupying that. It would take quite a while to drag and drop 116 of them into the array, so any quicker way?
You would do it by systematically adding them to the array, but I can't specifically give you the way to do it because I don't know the properties of your anchors. One way of doing it would be to set the tag of each anchor to "Anchor" and use the following.
void Start ()
{
GameObject[] g = GameObject.FindObjectsWithTag ("Anchor");
anchors = new Transform[g.Length];
for (int i = 0; i < anchors.Length; i++) {
anchors[i] = g[i].transform;
}
}
Upon using that, I receive this error:
Assets/AnchorPoint$$anonymous$$anager.cs(18,17): error CS0029: Cannot implicitly convert type `UnityEngine.Vector3[]' to `UnityEngine.Transform[]'
Edit: Here is my code: using UnityEngine; using System.Collections;
public class AnchorPoint$$anonymous$$anager : $$anonymous$$onoBehaviour
{
public GameObject gObject;
public Vector3 visualOffset;
public Transform[] anchors;
public float snapDistance;
GameObject visual;
bool isPlaced;
RaycastHit hit;
void Start ()
{
GameObject[] g = GameObject.FindGameObjectsWithTag ("AnchorPoint");
anchors = new Vector3[g.Length];
for (int i = 0; i < anchors.Length; i++)
{
anchors[i] = g[i].transform;
}
}
void Update ()
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit))
{
if (!visual)
{
visual = (GameObject) Instantiate (gObject, hit.point + visualOffset, Quaternion.identity);
}
else if (!isPlaced)
{
foreach (Transform anchor in anchors)
{
if (Vector3.Distance (visual.transform.position, anchor.position) < snapDistance)
{
visual.transform.position = anchor.position;
}
else
{
visual.transform.position = hit.point + visualOffset;
}
}
}
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.G))
{
Instantiate (gObject, visual.transform.position, Quaternion.identity);
isPlaced = true;
}
}
}
}
After going ahead and testing this, there were a few strange issues. The script is now fixed, the answer updated, and it should work properly.