Doubt about placing items on tables
Hello everyone. I have a question about how to place items on a table exactly in the center of it in a fixed way, which the player can pick up or place whenever he wants. An example is as seen in the game "Overcooked"... seems like its works like a grid.
What should I do? I already have the item with rigidbody, and the table also, both with colliders. The player already collects the item as well.
Another example I made...
Thank you all!!
Hi,
I don't know any better solution for now. But the following is the first approach hit in my $$anonymous$$d: When you need to place the item on the table or any object.
$$anonymous$$ake Child of the Object.
(With the help of little math calculations) Get the bounds of the object (like a table) to the height of the table and ADD object's height (which need to be placed ) so that it should come at the top.
You can skip the above step if your player always holds the objects with very much. Because after dropping the objects it will always come at the top.
But now as I'm writing this answer there are numbers of questions co$$anonymous$$g in my $$anonymous$$d. Like: How you want to show the effect?
Let's say you have the ball then you need to show the bounce effect or not.
Is your game grid-based or freely movement character is present?
If it is grid movement. Suppose the player have a Cube now he wants to Drop the cube on the table. You want to instantly snap the cube to the table or it's a slow motion effect.
Lots more questions are present in my $$anonymous$$d now. So Sorry If I confused you. But there are plenty of methods. which may vary upon the requirements of the game.
In the reference game ( overcooked ) they have used the snapping technique which is very simple.
Just detect the table ( like by making the collider bigger from the top side) or on the which item is get triggered in the Collider of the dropping object ( let's say ball in this case)
$$anonymous$$ake child
- To remain at always on top and which proper snap position. I have two approaches:
I will put one empty object on above the table. This empty object is placed in such a way so that I can give his positions to the ball. So that it always looks pretty cool and on the table with proper alignment.
Generic way. Find the height & center of the table from the Bounds. Add the scale of the ball in it. It will return you a position on which you have to place the ball. ( Sorry it's hard for me to explain this formula) .
@itsharhdeep
Thanks for the attention :)
Its exactly what you said, Im trying to make a simple project tha uses the logic of Overcooked (I'm a fan), putting items on tables, manipulate them, grab and drop all the time over the stage. The player is free to walk over the place, but the items on tables stay fixed.
I did what you said, I put a empty gameobject above the table, parent the item to the table and pass the position/rotation to item of empty gameobject... That's ok but, assu$$anonymous$$g there will be different sizes of items, some of them keep "floating" or "inside" the table, because there is no direct contact to table...
So... the second way, the generic way as you say... The script is too complicated to implement the formula?
I have made a basic script which might help you to get the bonds and make the child. The script is attached in the answer's section. If you need I can also send the package which contains the TestScene.
Answer by itsharshdeep · Nov 14, 2016 at 08:16 PM
Following is the script I have made in the hurry up. You can check this. I tried to set random scale and positions and it worked. But there might be the special cases which need to consider as per the requirements
using UnityEngine;
using System.Collections;
public class TableChild : MonoBehaviour
{
MeshRenderer meshRenderer;
void Start ()
{
meshRenderer = gameObject.transform.GetComponent<MeshRenderer> ();
}
void OnCollisionEnter (Collision col)
{
MakeChild (col.gameObject);
}
private void MakeChild (GameObject go)
{
//getting externs bond
Vector3 goBonds = go.GetComponent<MeshRenderer> ().bounds.extents;
//Setting the positon of the object
go.transform.position = new Vector3 (gameObject.transform.position.x,
meshRenderer.bounds.extents.y + goBonds.y + gameObject.transform.position.y,
0);
// After setting the position then making the child
go.transform.SetParent (gameObject.transform);
go.GetComponent<Rigidbody> ().isKinematic = true;
}
}
Its works here!
I made some modifications in it for my project and it worked well.
Thank you so much @itsharshdeep
:) No Problem Happy to help :)
You can let us all know the changes you have made in the script if they are generic and required to make the script more stable and usable.
Your answer
Follow this Question
Related Questions
How to move an object on a terrain that will always stay on top of the terrain? 2 Answers
How to fix this problem? 1 Answer
How To how ?? 2 Answers
Collision Detection with DrawMeshInstanced 0 Answers
Ignore collision based on position 1 Answer