- Home /
Building in Unity
Alright so i have a drag and drop script, a gui that makes parts, and the parts that i want to go together but i dont know how to make it so that if you drag the part close enough to a cretin spot it will snap t it and if i let go of click it will place it there because im making a game with similar building mechanics to Kerbal space program can anyone help me with that?
Answer by Mmmpies · Dec 03, 2014 at 08:58 PM
No idea about Kerbal or cretin spots but if you have a collider on the part you want to snap to and a collider on the part you to snap in that place you should be able to do an onMouseUp event that then sets the transform of the snapto part to = the transform of the part you want it snap to. Man that's a mouthful!
Or, having reread your post!, when the colliders collide snap one part to the other. All you really need are two empty game objects one in each part and then set the transform of the one you want to snap in place to equal the transform of the one you want it to snap to. Just place the empty game objects in the models so when they match up they look right.
I've not tried this myself but the theory is sound, if you're not sure how to do that let me know and see if I can put a script together as it's not 100% different from attaching a sword or gun to a characters hand. I only work in C# though.
Oh and just look up colliders and attaching a weapon with the word unity in the search on YouTube and I'm pretty sure you'll find exactly what you need.
I have no clue where to start with this stuff so thanks for the help i also found this which you might want to look at befor trying to help http://docs.unity3d.com/ScriptReference/Vector3.Distance.html
Yeah I know about Distance, a collider is really just that. A box or sphere or other the checks if the distance has been breached.
I've got a basic setup but it looks like I can't upload a unity package.
There are 3 scripts. One on the higher object (I watched the vid on your other post after you commented on my question) and one on the lower and one from another post on here that goes on both.
It's not exactly what you want but hopefully will give you head start.
so on the upper object like the capsule:
using UnityEngine;
using System.Collections;
public class SnapTopToBottom : $$anonymous$$onoBehaviour {
public GameObject SnapBottom;
public GameObject $$anonymous$$e;
private Transform otherSnapTop;
private TopSnap otherTopSnapScript;
void OnTriggerEnter(Collider other)
{
Debug.Log (name + " tiggered " + other.name + " " + other.transform);
otherTopSnapScript = other.GetComponent<TopSnap>();
otherTopSnapScript.SnapToBottom($$anonymous$$e);
}
}
then on the lower object (like fuel tanks)
using UnityEngine;
using System.Collections;
public class TopSnap : $$anonymous$$onoBehaviour {
public GameObject SnapTop;
public float offSet;
private Transform tempTrans;
private bool Snap = false;
private SnapTopToBottom sTTBscript;
void Update()
{
if(Snap)
{
tempTrans = sTTBscript.SnapBottom.transform;
SnapTop.transform.position = new Vector3(tempTrans.position.x, tempTrans.position.y + offSet, tempTrans.position.z);
}
}
public void SnapToBottom(GameObject Bottom)
{
Snap = true;
sTTBscript = Bottom.GetComponent<SnapTopToBottom>();
}
}
Finally on both:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof($$anonymous$$eshCollider))]
public class GizmosController : $$anonymous$$onoBehaviour
{
private Vector3 screenPoint;
private Vector3 offset;
void On$$anonymous$$ouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void On$$anonymous$$ouseDrag()
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
}
}
shame I can't upload the unity package so you can see that, let me know if you know a way.
PS this is a short vid of it working. I've seen people upload unity packages to youtube but can find no tutorials on how to do that!
Tanks for all the help you are awesome and if you add me on skype then you can sent files and it would make it easer to talk my skype is zxynine just so you know there are two zxynine's they are both me so just send a message to both
Had issues with Skype but just got a dropbox account so I can share this:
Note it's not a full solution so it will need work, but it'll give you a head start.
Can I ask a question, well I guess I just did but are you new to Unity and program$$anonymous$$g?
If so then my advice would be go on youtube and search for beginners guide to unity and to unity c# (or java if you prefer).
I'm not trying to be nasty it's just you'll get a lot more people helping if you've attempted the code in the first place.