Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ltrout1999 · Oct 10, 2016 at 03:59 AM · c#positioningsnappingtower-defense

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ltrout1999 · Nov 12, 2016 at 02:55 AM 0
Share

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?

avatar image Namey5 ltrout1999 · Nov 12, 2016 at 03:03 AM 0
Share

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;
     }
 }
avatar image ltrout1999 Namey5 · Nov 12, 2016 at 03:13 AM 0
Share

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;
             }
         }
     }
 }
 
Show more comments
avatar image Namey5 · Nov 12, 2016 at 12:56 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

245 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help with snapping Prefab assets to grid in game 2 Answers

Buy system - transform.position bought object 0 Answers

Positioning Screen Space UI element in center of a grid square? 1 Answer

Calculate a point in space based on a triangle 0 Answers

Clicking on a 2d image and placing 3d object? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges