- Home /
how to I zero out a parented child's position in X,Y,Z on collision?
Im building a stacking game with objects falling off of a conveyor belt and stack on a platform the player controls. I have the object that falls onto the platform and on top of each other item child to the falling object and apply a Joint Spring. I want the object that is falling to become a child to the platform or object that is stacked on the platform, then Zero out the position of the child object's XYZ coordinates then lastly apply the Spring Joint.
Here is what I have so far.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class IngredientsJointConnect : MonoBehaviour
{
SpringJoint springJoint;
bool hasJoint;
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.GetComponent<Rigidbody>() != null && !hasJoint)
{
gameObject.transform.parent = collision.transform;
gameObject.AddComponent<SpringJoint>();
gameObject.GetComponent<SpringJoint>().connectedBody = collision.rigidbody;
hasJoint = true;
springJoint = GetComponent<SpringJoint>();
springJoint.spring = 900;
springJoint.enableCollision = true;
springJoint.breakForce = 150;
}
}
}
what do I need to add to have the Position zero out?
Thanks!
Answer by DawdleDev · Oct 02, 2018 at 10:41 PM
Either
transform.localPosition = Vector3.zero;
Or
transform.position = Vector3.zero;
One of them should do the trick.
Thanks! my objects are appearing under each other though as apposed to wanting them to stack on each other, would you know how to fix that? I was thinking I will need to add +1 in the transform y position every time there is a collision.
here's a video of the problem