- Home /
Creating objects and positioning next to a previous one
Hi,
I've searched a lot and tried different approaches to this problem, and I still can't come with a correct solution.
This is what I want to accomplish:
Randomly create an object based in some imported fbxs I have in a folder
Spawn the object in the scene stuck to another one that it's already in the scene.
Create a single object using both the previous one and the new one
Do this again many times.
What I am having problem with is sticking an object next to another one. I have used Ray to detect Collision and snap one to another. I've tried with Bounds, with Collider.ClosestPoint(), etc...
My first approach was to detect a point in my FirstObject's Mesh and then detect another one in my NewObject's Mesh and move the NewObject to place one point on top of the other.
if (Physics.Raycast(m_newObject.transform.position, m_savedObject.transform.position - m_newObject.transform.position, out hit, Mathf.Infinity))
{
SavedPoint = hit.point;
}
if (Physics.Raycast(m_savedObject.transform.position, m_newObject.transform.position - m_savedObject.transform.position, out hit, Mathf.Infinity))
{
NewObjectPoint = hit.point;
}
if (SavedPoint != Vector3.zero & NewObjectPoint != Vector3.zero)
{
m_newObject.transform.position += SavedPoint - NewObjectPoint;
}
Any idea or correction is welcomed.
Regards
Note: Both objects have Mesh Colliders.
Note: All the objects I have in the folder have different sizes and are complex meshes, not some regular primitives. I need a general solution to work with any object. I need to snap two objects in runtime without intersecting them.
complex meshes like tree roots or a factory? why don't you just set radiuses for each object?
Hi, thanks for your answer. I cannot set a radius because I need the meshes to be touching one to another. With complex meshes I mean not a sphere or a cube or a cylinder. Not simple primitives but more complex geometrical objects.
Set $$anonymous$$imal intersection radius. Instantiate. On Trigger enter in case of collision move outwards with lightning speed and stop on trigger exit
Hi,
Thank you for your answer. I've tried your method before. The problem with it is that is not perfectly accurate. I need two faces to get exactly stuck one to another. And with movement, velocity and collisions there is a lot of errors, objects overlapping, and it is dependent on the speed. I need something that allows me to teleport one object exactly next to another, touching it. Any idea would help. Thanks.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
I am having a weird issue with Quaternion.FromToRotation 2 Answers
C# Raycast Code not working 2 Answers