I’m making a rogue-like, and I want a damage system that uses variables from an attacking monster and a defending one. If that’s a terrible explanation, I’d like something similar to Pokémon Mystery Dungeon. I’m using UnityScript for this BTW.
I’d like an explanation of how I can do it, rather than just a script by itself.
Sorry, what’s your question? What exactly is it you don’t understand how to do?
I want to be able to use the variables of an object within a certain location relative to the player. As an example, if an enemy attacks and there is a player in front of it, the player should take a certain amount of damage based on the enemy’s attack stat. If there’s an enemy with a different attack stat, the player should take a different amount of damage.
There will be many different monster, most of which, will be used as enemies and possible player character. Each with their own stats and set of attacks. I feel like raycasting will help me solve this, so any good information on that?
Ah. So the question is: from a script on one object, how do you find nearby objects that might be in front of it?
Raycasting is certainly one approach, but you’ll have to cast a whole fan of them, fairly densely to avoid missing anything.
A better approach might be to define one or more colliders (e.g. CapsuleCollider) on your player. Position these out in front of the player, covering the zones you care about, and set them as triggers. Then you can simple use OnTriggerEnter to see when enemies move into this zone.
Or, you can directly call something like Physics.CapsuleCast to check for what’s in an area at any time within a script, and not have to do any bookkeeping yourself.
Of course if this is really a rogue-like game, there’s probably a world grid. In that case, you should skip all this complex collision-checking and simply keep track of what’s in each spot of the grid in a 2D array, allowing you to find nearby objects directly.
Or use a sphere trigger collider to maintain a list of nearby objects and check the dot product of the player’s local forward and the normalized direction from the player to the object in order to determine if it’s in front of you or not.
Thanks, but is there a way to check the enemy’s/object’s variables after it enters the zone or is detected by the raycast???