Quantcast
Channel: Questions in topic: "javascipt"
Viewing all articles
Browse latest Browse all 1875

Building a Snap System, Need Help

$
0
0
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!

Viewing all articles
Browse latest Browse all 1875

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>