- Home /
How to snap objects in game
I been looking for some script for assembly objects in a puzzle 3d game, my original idea is make this parent emptys to the big objects & add the snap script for this empty, with this build a bigger object. this is an example what I want to do, but without th leap motion & the bike.
http://www.youtube.com/watch?v=MKz7DdEH9mQ
Is there any example for make this kind of snapping? or do you have any idea for snapping via scripting? Thanks!!
The actual snapping in the video is pretty simple, but there are number of issues leading up to the snap. For example, without leap motion, how do you plan to deal with the 'z' axis? Or is this really a 2D game using 3D items? Are items only added to a whole like in the video or do individual items also snap together? Drag and drop is notably harder than the snapping code. Do you have drag and drop working?
thanks for replay, in fact I want to make this in a tablet devices for snap blocks, this blocks will be in front and the main figure will be in background, this game will be in 3d for assembly diferents figures dragging & drop this pices. actually drag & drop I found more examples and documentation to solve it. Thanks again & I'll wait for a tip or example.
If this is 3D, how do you plan to deal with depth? Fingers/screen are 2D. I assume you are using a perspective camera?
Answer by robertbu · May 08, 2014 at 10:11 PM
Here is and example script. To make it work you need to pair each part with game object. It may an empty game object. When you get close to that part (as viewed from a perspective camera), it will change color. To figure out close, the code projects the 3D position of the part and the partner game object onto a viewport and uses Viewport coordinates to do the calculation. If you drop the part while it is close, it moves and rotates into position as defined by its 'partner' object. Note that the color change depends on have a shader that supports material.color. Also it there is no drag and drop here and therefore must be done in a separate script.
#pragma strict
var partnerName = "Hub";
var closeVPDist = 0.05;
var moveSpeed = 40.0;
var rotateSpeed = 90.0;
var closeColor = Color(1,0,0);
private var dist = Mathf.Infinity;
private var normalColor : Color;
private var partnerGO : GameObject;
function Start() {
normalColor = renderer.material.color;
partnerGO = GameObject.Find(partnerName);
}
function OnMouseDrag() {
var partnerPos = Camera.main.WorldToViewportPoint(partnerGO.transform.position);
var myPos = Camera.main.WorldToViewportPoint(transform.position);
dist = Vector2.Distance(partnerPos, myPos);
renderer.material.color = (dist < closeVPDist) ? closeColor : normalColor;
}
function OnMouseUp() {
if (dist < closeVPDist) {
transform.parent = partnerGO.transform;
InstallPart();
}
}
function InstallPart() {
while (transform.localPosition != Vector3.zero || transform.localRotation != Quaternion.identity) {
transform.localPosition = Vector3.MoveTowards(transform.localPosition, Vector3.zero, Time.deltaTime * moveSpeed);
transform.localRotation = Quaternion.RotateTowards(transform.localRotation, Quaternion.identity, Time.deltaTime * rotateSpeed);
yield;
}
}
Ok, I'll test it rigth now!!! Thanks again, soon I'll coment the results.
Hi @robertbu, for some unknown reason I haven't been able to work your "example script" for HTC Vive controls. Do you know some package in Unity Asset Store that includes this function for Vive controls?
Very grateful in advance for your advice.
Your answer
Follow this Question
Related Questions
How do I snap/assemble two or more oddly shaped objects together? 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to drag gameobject (only x y) snapped to a grid? 2 Answers
Snapping Objects Together in 2D Scene 3 Answers
Character snap to camera direction 0 Answers