- Home /
Help with snapping and "gluing" parts together
Hopefully you guys can help me out, I've been stuck on this issue for a lot longer than I'd like to admit.
I'm building a VR freeform Block Building game (basically a VR Lego freebuild game) and I need help on how to snap the parts to each other. This is the last thing I need for this game to be finished and it's driving me nuts. I just started in unity about two months ago and I'm not really familiar with C#.
Here's the basic hierarchy of how the parts are modeled:
Empty Object: Rigidbody with gravity (untagged (yes it shows Nub tag but I changed it since the screenshot))
Base rectangular shape: box collider
Nubs: Sphere collders as triggers, "snap" script, NUB tag
BNubs (where the nubs would snap to): sphere colliders as triggers, "snap" script, BNub tag
Here's a picture as well:
What I want to do is when a collider of the top nubs (Nub) enters the collider of the bottom nub (BNub), I want the Nub to snap into BNub's position and set the Nubs x&z axis rotation = to BNub's x&z axis. This would snap the nub into place and align it, making it a concentric mate. If 2 Nubs entered 2 BNubs it would essentially line itself on the y axis. This way if only 1 nub was connected, it could still pivot on the y axis.
This is all I can come up with for the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Snap : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter(Collider brick)
{
if (this.gameObject.tag == "Nub" && brick.tag == "BNub")
{
transform.parent = brick.gameObject.transform;
}
}
}
As you can see by the code so far, when Nub collides with BNub, Nub switches it's parent from the brick to the BNub and glues itself where the colliders first hit, which isn't quite what I want but shows the 2 tags work so that's a plus.
When I try this code:
transform.parent.parent = brick.gameObject.transform;
The entire Brick becomes a child of whichever BNub it interacted with but because of gravity it doesn't stick to the brick. It still moves when I move the Parent brick but it drags on the floor instead of being glued to the brick. Also when I move the child brick, the parent brick doesn't move.
Here's what I need help with:
How can I snap the child Nub position to BNub position and have the whole brick (parent) move?
How can I align Nub's X & Z axis to BNub's X & Z Axis while aligning the parent?
How can glue them together and have them both move as if it were a single object, no matter which brick was selected?
Thanks for any help you guys can give me and hopefully I was clear enough. It's been giving me a real headache and is way beyond my scope of understanding at this time.
Did you ever get anywhere with this? I've actually just started a very similar project and am running into similar hurdles.
Answer by gibsondude · Apr 20, 2020 at 08:54 AM
I'm currently struggling with this too :/
Did either of you manage to work this out?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to move child with parent within the same frame. (Before trigger events) 2 Answers
Making a bubble level (not a game but work tool) 1 Answer
An OS design issue: File types associated with their appropriate programs 1 Answer