- Home /
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!
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
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.
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?
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!
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
Follow this Question
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