- Home /
Problem was different than the one asked
How to limit the instantiation of a prefab/object?
I will explain this in the best way I can.
I want to be able to instantiate an object(or a prefab) in a certain direction until it collides with a gameobject that has the "Wall" tag. When it hits the "Wall" gameobject I would like it to resize the object so that it will never pass through the wall.
Is this possible in any way? Any tips or hints would be appreciated too!
Thank you, Eugen
sugges unityGE$$anonymous$$S.com for beginner articles on physics, etc.
Answer by Mortoc · May 09, 2013 at 01:49 AM
If you keep track of your "wall" or plane as point-normal, it's really easy (Khan academy link for the math involved). The code would look something like this:
Vector3 wallToObject = wall.position - object.transform.position;
float dot = Vector3.Dot(wallToObject, wall.normal);
if( dot > 0.0f )
// object has passed the wall this frame, move it back and instantiate your collision object
I'm not sure that is what I'm looking for.
I am normally instantiating more than 1 object (at least 2, sometimes even 10 instances) and the Dot gives me vector3 coordinates while at any given time I am going to be interested in the y or x position. The "z" does not matter for the project I am currently building.
Any work arounds for this?
I tried adding a collider on the prefab that I am creating instances of and a script that tells it to stop when it hits the top but it doesn't seem to work.
Edit: I managed to get it to see the collision by adding a rigidbody (my bad) still not working but I am going to ask another question because the problem changed.
EDIT 2: Your script almost worked, but I managed to fix it by simply conditioning it to a variable in a separate script! $$anonymous$$arked as correct as I probably would have solved it by using this as well.
Follow this Question
Related Questions
Cloud objects not instantiating (Oh no!). 0 Answers
Why is it important to create an empty gameobject for my prefabs? 0 Answers
Instantiate as child of a to-be-instantiated list item 1 Answer
Instantiating in Awake() v Setting up prefab instances in Editor performance difference? 1 Answer
Checking if object intersects? 1 Answer