- Home /
Stacking mechanic (Parenting Prefab to another prefab on collision)
Hello i'm working on building a stacking mechanic for my game.
what I want to happen is for my movable platform to catch and stack the falling objects as they collide with the platform. My idea was to make the falling objects a child to the platform when a collision between them happen and then it would "stick" to the platform while the player is still able to control the platforms side to side movement.
this is the code I am using at the moment, im able to get the platform to child the falling objects after a collision between them, but it wont stay sticking to the platform when I move around.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Stacking : MonoBehaviour
{
private void OnCollisionEnter(Collision stacking)
{
if (stacking.gameObject.tag == "track 01")
{
stacking.transform.parent = GameObject.Find("BurgerPlate").transform;
}
}
}
any suggestions or other ways to accomplish this task?
Thanks in advance!
Answer by TreyH · Sep 19, 2018 at 08:05 PM
Can you just disable the physics on it?
private void OnCollisionEnter(Collision stacking)
{
if (stacking.gameObject.tag == "track 01")
{
stacking.transform.parent = GameObject.Find("BurgerPlate").transform;
// It's moving because of its Rigidbody.
stacking.transform.GetComponent<Rigidbody>().isKinematic = true
}
}
Yep what he said should work - or you could also just remove the rigidbody altogether if you wanted to save a little bit on background calculations
private void OnCollisionEnter(Collider stacking)
{
if (stacking.gameObject.tag == "someTag")
{
stacking.transform.parent = this.transform;
//or whatever object you want to be the parent
gameobject.destroy(stacking.getComponent<rigidbody>());
}
}
Answer by Winnppl · Sep 19, 2018 at 08:40 PM
Thank you both! I implemented parts of each of your codes and it worked perfectly!
Much appreciation!
Be sure to mark the best answer as the correct answer so it gets flagged as solved. Cheers!
Your answer
Follow this Question
Related Questions
Activate trigger if items colected 1 Answer
If gameObject's center passes through a trigger? 1 Answer
Play Animation OnTriggerEnter 2 Answers
Targeting the GameObject I collide with? 1 Answer
Whats wrong with my score script 1 Answer