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 /
avatar image
0
Question by Rebel8909 · Jan 16, 2016 at 04:16 AM · javascriptcolliders

Building a Snap System, Need Help

So I have a basic snap system kind of set up. I have a parent cube with 4 other cubes around it at 1 unit that act as connection points. All connection points are children of the middle cube. Half of them work and the other half sort of work.

The left and top cube snap perfectly fine, the bottom and right cube only half work, as in they won't snap with the initial click, if the box I want connected collides with the bottom or right block it ticks a box saying there's a collision but doesn't snap. However if you leave the two cubes colliding and click away from the cube it snaps. It'd kinda of odd and I've been fiddling with it for a few days now. I also have a problem if the center cube gets moved and I try snapping anything the cubes snap to the world coordinates (IE 0,-1,0 or 0,1,0) instead of the cube I'm trying to snap it to

Examples -

Cubes Half Snapping : http://giphy.com/gifs/3oFyD9MzKPJ9GZ2hDa?status=200

Cubes Snapping to a different spot : http://giphy.com/gifs/3oFyCZXElW7cO0DYoU?status=200

Here's the code :

 #pragma strict
 var connection : GameObject;
 var coord : Vector3;
 var collided : GameObject;
 var Atom : GameObject;
 
 var isParent : boolean;
 var on : boolean;
 
 function Start () {
 on=false;
 connection = this.gameObject;
 coord = connection.transform.position;
 
 if(connection.gameObject.tag == "Atom"){
     isParent = true;
 }
 
 if(isParent == false){
     Atom = connection.transform.parent.gameObject;
     }
 }
 
 function Update () {
 if(isParent == false){
     if(on == true){
         if(Input.GetMouseButtonDown(0)){    
             Snap();
             }
         }
     }
 }
 
 function OnCollisionEnter(col : Collision){
 
     if(col.gameObject.tag == "Atom"){
         collided = col.gameObject; 
         Debug.Log("Collision with an Atom");
         on = true;
     }
 }
 
 function OnCollisionExit(){
     on = false;
     collided.transform.parent = null;
     collided = null;
 }
 
 function Snap(){
     collided.transform.position.x = coord.x;
     collided.transform.position.y = coord.y;
     collided.transform.parent = Atom.transform;
 }


Any help is appreciated, I know I'm probably missing something super obvious but maybe it's not. Thanks!

Comment
Add comment · Show 2
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 Fattie · Jan 16, 2016 at 03:48 PM 1
Share

Just FYI if you're trying to program $$anonymous$$inecraft, it just has little or nothing to do with these concepts, it works differently. Note that there are many completely free and also paid "do $$anonymous$$inecraft in Unity!" packs out there, just google.

Regarding snapping cubes, I'd really just look for a package on the asset store if yer a beginner, it's pretty hard

avatar image Rebel8909 Fattie · Jan 16, 2016 at 05:25 PM 0
Share

It's not a $$anonymous$$ecraft in Unity thing. It's going to be a building DNA game eventually. Just needed to get the basic functions down first. I mean as you can see I have it almost working, it just needs a little fleshing out.

1 Reply

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

Answer by Bunny83 · Jan 16, 2016 at 08:49 AM

You assign the vector which is stored in "coord" to the worldspace position of your object. "coord" seems to be some kind of relative offset. You might want to do:

 function Snap(){
     collided.transform.parent = Atom.transform;
     collided.transform.localPosition = coord;
 }

or

 function Snap(){
     collided.transform.parent = Atom.transform;
     collided.transform.position = Atom.transform.position + coord;
 }


It's not really clear to which object this script is attached to and how many objects are actually involved. Are your snapping points also gameobjects? Why don't you snap to the position of those objects?

Comment
Add comment · Show 2 · 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 Rebel8909 · Jan 16, 2016 at 05:23 PM 0
Share

The script I've shown is attached to the green boxes, which are gameObject children of the center box. I'll try those two bits of code you gave me and get back to you!

avatar image Rebel8909 · Jan 16, 2016 at 05:29 PM 0
Share

O$$anonymous$$,

 function Snap(){
     collided.transform.parent = Atom.transform;
     collided.transform.position = Atom.transform.position + coord;
 }

Works perfectly! It keeps the connected object as a child and fixes it going back to the odd location. Would you happen to also know why the bottom and right connection points don't seem to work with the first click?

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

38 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

Related Questions

Accessing Colliders in IF statement from PARENT Object. 1 Answer

How to make the player turn left or right only in desired places o edges and not everywhere ???? 0 Answers

How to detect when two gameObjects collide by name {java, 2d} 2 Answers

Switching colliders without falling through the world? 1 Answer

physics.OverlapSphere colliders 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