Hi there. I’ve been stuck for several days because I can’t solve this. I wanted to be able to solve it on my own but I’m not being able to. I need my character to rotate towards the closest objects that are within his “view range”. This implies that they are not behind a wall.
I have tried many ways and variations. I have a character and objects that the character must detect. The group “Group3” contains the objects that the character must detect. The group “Group2” contains the objects and the wall to test the ray.
I know that choosing the closest object in the first instance is that it only emits the ray to that object, but so far it is the least worst of the attempts. At least this way it turns to the closest object that isn’t behind a wall. The problem is that if there is an object close but behind the wall and another one a little further away but uncovered, it does not detect it because it emits the ray to the object behind the wall (it does not rotate towards any object in this case). Everything I tried so far was by ray emission.
I used “For each object of Group3” as well and no differences. It also makes no difference if I don’t store the position in Variable(CharacterX) and Variable(CharacterY), replacing the position to rotate with Group3.X() and Group3.Y() respectively.
It doesn’t work well in this form either, it doesn’t rotate to the nearest object.
In the basic IA example it doesn’t work with multiple objects, only one.
Your help will be very welcome. Oh…and I forgot…in addition to the fact that I need to solve this, it must be in a way that doesn’t consume excessive resources haha.
Something like this will do what you want :
You’ll need to change Enemy with Group3,
The group RaycastGroup has objects Wall and Enemy as members.
Thanks for the reply. I’ve been trying, but sometimes the character just freezes for a while and then keeps spinning. Many times it starts to spin and speeds up the spin rate (I guess the spin event is played more than once). I’m trying to make modifications to this to see if I get a better result. Did it work fine for you this way?
I’d recommend checking the “Basic AI with Pathfinding” example. It basically does this exactly (although the inverse, has the enemies rotate towards the player if the player is not behind a wall). They even use a vision range.
You’d basically do the same thing but have the enemy in the object group instead of the player. Just make sure there’s a parent event that pick’s all enemies and enemies within X distance of the player.
The events I posted worked for me. the player rotated at 150 degrees per second towards at an event rate the nearest unobstructed enemy.
Thank you both for your answers.
Yes, I have been analyzing it and I have mentioned it but it works if the object on which the ray is emitted is only 1. It is a good idea to do the reverse. The drawback is that I must associate actions to the character when he is turned towards an object that is not covered by a wall. In this case, it would be difficult for me to associate actions if it is not also emitting a ray from the character, which would make the code more complex, I think. And things end up being the same if there is more than 1 character, which is the idea. I did not mention it before because doing it the other way already contemplates this inconvenience.
At this point I’m starting to think I’m going crazy haha .This is what I have been doing. I edited it into a new scene to avoid that maybe there is some event somewhere else that is affecting it and I didn’t realize. I clarify that the character has the draggable behavior to make it easier to move it closer to and away from objects.
I thought that the problem could be that Enemy in your case is an object and not a group, but by replacing everything with an object (adapting the group on which the ray is tested to this situation), the result is the same, the character doesn’t always select the closest object and many times the only way to stop looking at one selected object is if it gets far enough away or is obstructed by a wall. And a lot of times the character is AFK (perhaps doing mathematical calculations). After a while it continues, and several times it rotates at double or more speed.
Is Group3 a member of Group2, or are the objects of Group3 all in Group2? If not, then that will most likely be your problem.
“Group3” contains all the objects that the character must identify and “Group2” contains all these objects and also the wall.
Weird, I’m not sure I understand why that is.
Anyway, here’s a fix for it:
It seems weird to me too. I’ve been testing it this way and it works perfect for me, thank you very much!! I was also testing with the debugger to evaluate the consumption of resources and it fits perfectly with the parameters that I was looking for in terms of the time needed to process the code. Thanks again for your time, I’ll mark it as resolved.