- Home /
Finding an object is within an another
Hi all,
This is my first post on the forums and I'm still pretty new to Unity so i have a small issue. I have a main script that governs how objects move within the screen. It uses a character controller to accomplish this. Then in an inherited class i specify how the move is set. On the player i grab the input like oh so many tutorials and on any enemy that moves i try to find if a player is within a certain range, if he is i look at him and create a vector towards him.
if(isActive)
{
transform.parent.LookAt(new Vector3(
trPlayer.position.x,
transform.parent.position.y,
trPlayer.position.z));
moveParse = trPlayer.position - transform.position;
parentMove.move = moveParse.normalized;
}
else
parentMove.move = new Vector3(0.0f, 0.0f,0.0f);
The enemy has a child which is a sphere when the player enters this sphere it functions as a trigger and it communicates to the enemy where the player is within this Field of View.
This works pretty good most of the time. The big problem starts when i duplicate the enemy, since they will begin climbing in-top of each other. I have tried to find many ways of fixing this issue but im starting to think my approach is flawed.
I read somewhere that a similar way of doing this is by using ray cast but i believe this would be more expensive (process wise) since each ray must be checked for collisions and instantiated.
So how would i go about stopping the objects from climbing each other?
Answer by strachpr01 · Aug 01, 2012 at 08:03 PM
could you try adding an extra boolean so if another enemy enters its sphere to stop moving until it is gone therefore they would not be able to 'climb' over one another.
Answer by SantiMuffin · Aug 01, 2012 at 10:20 PM
the problem is that i need them close enough as to actually stop the player from being able to advance. But maybe adding another sphere collider that is the size of the enemy will allow me to implement this. How expensive can a solution like this be when it comes to resources? Wont it be checking for more collision?. (Thinking that this will be for a mobile device).
When you say you wish for the player not to be able to proceed. Are the enemies constantly moving towards the player or are they moving into a set position to block the player at a bottleneck in the game?
Right now they are constantly moving towards the player so as to block his path. Since the player will always come from the -z direction the idea is for them to try to block his path or go towards him to kill him.
Answer by SantiMuffin · Aug 08, 2012 at 11:16 PM
Just to close the matter I went with making a cube that is 10% bigger than my item and using it as a Trigger. :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613075028im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Resize Array Based on Value 2 Answers
how to make my character go round loops? 1 Answer
Unity can't find Character Motor and max forwad speed -1 Answers