Make object(s) stack vertically.
Hello, I'm working on Unity 3d / C# stuff and have a question. Here is the image of the current project.
The brown cube object on the left bottom is named "Player", which I can move anywhere in the Plane. Other yellow and light gray cylinders are named "Ingredient". and I attached Ingredient.cs script. In the script, there is an OnTriggerEnter event, so when the Player object collides with an Ingredient, I can do something.
What I want to do next is when the Ingredient collides with the player, place and stack the ingredient on top of the Player object, and then move with the Player object (kind of like making a tower.) Things I have done were making an empty object called "PlayerContainer", and when the Ingredient object is collided by the Player object, set Ingredient parent to PlayerContainer.
As the image shows, now I can make the Ingredient object parent to PlayerContainer, but I cannot stack the Ingredient object onto the player and move together.
public class Ingredient : MonoBehaviour
{
GameObject player;
GameObject playerCont;
GameObject topIngredient;
float playerHeight;
void Start()
{
playerHeight = 0f;
}
void OnTriggerEnter(Collider other)
{
PlaceOnTopOfPlayer(other);
}
void PlaceOnTopOfPlayer(Collider other)
{
player = other.gameObject;
playerCont = player.transform.parent.gameObject;
playerHeight = playerCont.transform.lossyScale.y;
iTween.MoveAdd(this.gameObject, iTween.Hash("y", playerHeight, "time", 1.0f, "oncompletetarget", this.gameObject, "oncomplete", "CombinedWithPlayer"));
}
void CombinedWithPlayer()
{
numOfIngredientCombined += 1;
this.gameObject.transform.SetParent(playerCont.transform);
}
So could you tell me how can I make the Ingredient object move together with the Player after this? Also, do I really need to put Ingredient object as a child of PlayerContainer?
※ I didn't add Rigidbody to Ingredient object but Player object.