Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by ratboy · May 03, 2012 at 06:07 PM · 2dgridtilesnapdragrigidbody

Drag Rigidbody into slots?

Hi - i know there are several questions like this, but none of them have a conclusive answer, and the only one that i could see and answer to has a broken link, in french, so i can't see it:

http://answers.unity3d.com/questions/204483/create-grid-and-snap-rigidbodies-to-grid.html

anyway, I have a 2D sprite based game, where you click on the objects you want to choose from the HUD and they spawn in the play area, I have a drag rigid body script on them and it works fine, no problems.

However, i now think it would be better if the drag rigid body script worked in increments, i would have a grid on the background, and the drag wouldn't be smooth, but would snap into the positions, each equally separated.

One way i could think of would be to put empty game objects on each point with a trigger area, and add a script on each trigger to say:

 function OnTriggerEnter(){
     draggedRigidbody.transform.position = this.transform.position
 }

etc,

BUT i think this is extremely complicated as every level will have a different shaped grid, and i don't want to have 30 triggers with scripts and have to set them up each level.. would there be a better way to go about this?

thanks in advance, by the way if there are any code samples posted, could they be in JS please.. still need to get round to start programming in C :p

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

2 Replies

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

Answer by easilyBaffled · Jun 02, 2012 at 03:24 PM

This is my full implementation of Click and Drag and Snap to Place. The Snapping bit takes place in the OnMouseUp section, basically, find the center of each slot on the grid, find the location of your Object and subtract from its position to get it to the closest center.

 var moving = false;
 var posX : float;
 var posY : float;
 var ray : Ray;
 
 function Update () {
     
     if(moving == true){
         ray  = Camera.main.ScreenPointToRay (Input.mousePosition);
         transform.position.x = ray.GetPoint(14).x;
           transform.position.y = ray.GetPoint(14).y;
           transform.position.z = 1.1;
     }
 }
 
 function OnMouseDown () {
    moving = true;
 }
 
 function OnMouseUp () {
     //if out of bounds destroy it
     if(transform.position.x > 1.5 || transform.position.x < -10.5 || transform.position.y > 9.5 || transform.position.y < -.5){
             Destroy(gameObject);
     }
       moving = false;
       //Snap to feature
       posX = transform.position.x;
       posX = Mathf.Round(posX);
       if(posX > transform.position.x){
           posX--;
       }
        posY = transform.position.y;
       posY = Mathf.Round(posY);
       if(posY > transform.position.y){
           posY--;
       }
 
       transform.position.x = posX +.5;
       transform.position.y = posY +.2;
 }
Comment
Add comment · Show 1 · 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 ratboy · Jun 30, 2012 at 10:15 AM 0
Share

sorry for the late reply, i ended up going in a different direction and not implementing this at all, now i haven't tested your script but the logic makes sense, thanks for the answer, this may come in handy in the future :P :)

avatar image
0

Answer by BigBlob · Jul 27, 2012 at 02:18 PM

In fact I speak french not very much but I accidentaly found the Download button so heres the direct link to it, tell me if it doesn not work!

LINK

  • Felipe

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to use the same tilemaps among multiple scenes (e.g. making it a prefab)? 0 Answers

How can I snap to hex tile centers in world x,z coords? 1 Answer

Grid Movement VS Free Movement 1 Answer

What to do with "unloaded" gameObjects? 1 Answer

Unity 2D tiles and grid 1 Answer


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