playMaker

Author Topic: units attack each other / strategy game [SOLVED]  (Read 1761 times)

Nietzsche

  • Playmaker Newbie
  • *
  • Posts: 26
units attack each other / strategy game [SOLVED]
« on: August 31, 2019, 12:28:05 PM »
I am sorry for asking so much, but I am pretty new to the whole substance of programming. So please forgive me for asking a bit more questions.

I am working on a small round based, hextile based game where units should be able to shoot at each other. I have just no idea how the shooting should be managed.

How can I make objects detect each other?
How can I make them shoot one annother? I would like to make a unit directly detect it's enemy and then hit it with a 100% chance. The projectile should also ignore anything in its path.

Sorry for the long post and thank you for your help.
If you know any good tutorials or guides on this subject please don't hesitate to tell me.
« Last Edit: September 03, 2019, 10:55:16 AM by Nietzsche »

Athin

  • Full Member
  • ***
  • Posts: 163
Re: units attack each other / strategy game
« Reply #1 on: September 01, 2019, 10:58:58 PM »
Heya,

So this is a really broad question which is very situational depending on the project.  Is the shooting just a % chance (Similar to Xcom Style games) or is it actually manually aimed as a FPS?  Does the unit have range or need a clean line of sight to attack?  There is a lot here to digest and not one simple post will be able to solve it. 

I'd suggest you break down the problem into steps and solve them in little chunks. 

Nietzsche

  • Playmaker Newbie
  • *
  • Posts: 26
Re: units attack each other / strategy game
« Reply #2 on: September 02, 2019, 12:49:38 AM »
sure, sorry for not breaking it down earlier.

- line of sight should not affect the shooting
- units have a fixed range
- hitting should be a %-chance (dependend on precision, unit movement, etc.)

Thank you for your suggestion, in the meanwhile I already solved how to do the shooting at oneanother.

daniellogin

  • Full Member
  • ***
  • Posts: 215
Re: units attack each other / strategy game
« Reply #3 on: September 02, 2019, 04:34:23 AM »
Judging by the nature of your questions, I think you may not be ready to take on this project. I don't want to be negative and sound like I'm saying don't try, but to me it suggests that you don't understand the fundamentals yet, or you would actually know how to frame your questions better.

How to make objects detect each other?
Do you know about colliders and triggers? You can check out a general Unity tutorial to learn more about how these things work.
A collider means objects can 'touch' each other, so a ball hits the floor and rolls, not just goes through it.
A collider set to be a trigger is like a ghost, so won't stop the ball from going through the floor, however it records the interaction.

Example; to make a ball hit the floor and not go through it, and play a sound when it hits, you can make two colliders. One tightly fits to the visual appearance of the ball, to represent it's physical form. The other is slightly larger and is set to be a trigger. Now when the outer (ghost like) trigger collider has the floor collider enter it, you can use a Trigger Event action to transition to a state which plays the sound.

This example could switch up the result to many possibilities, including recording damage information, etc. The Trigger Event can be set so it only counts interactions between the target object and a specific tag on another object, so for example it may only fire when it hits the object tagged 'player' or 'floor', etc.

How can I make them shoot one another?
There's more than one way to skin a cat. The method you choose will largely depend on other factors in your game. For example you may create an object which moves towards the target, and registers when it hits, or you could use Ray Cast which is instant, OR maybe no real 'firing' at all. I mean, do you even need a 'real' projectile? You said it hits 100% chance right? Well, maybe don't even 'shoot' anything at all. Simply update the health information when the right time comes, and then pair it with some animations / objects / particles. The objects or particles created don't even have to register the hit. If it's just a muzzle flash for example, just do that while doing an Int Add or what ever that updates the health.

I would like to make a unit directly detect it's enemy
This again depends on context, and there are very many ways to do each option. For example with an FPS, you would need to consider the AI behavior to path find and move around, rotate to face you and only know you are then when facing you, etc. However if it's like a board game where not all the dimensions are used, there is no line of sight, etc, you can do something a lot more simple where it simply gets the distance, uses a big square collider which snaps with the grid, or reads it's location on the grid relative to the target, or etc.

Basically your first difficulty at the moment is like you are asking how to do a barrel roll in an air craft, but you haven't even made the plane yet. There's no proper answer we can give, since using my above example, we don't know if your air craft is even a helicopter, a jet, a paper plane, etc.

Nietzsche

  • Playmaker Newbie
  • *
  • Posts: 26
Re: units attack each other / strategy game
« Reply #4 on: September 02, 2019, 07:35:22 AM »
Quote
There's more than one way to skin a cat. The method you choose will largely depend on other factors in your game. For example you may create an object which moves towards the target, and registers when it hits, or you could use Ray Cast which is instant, OR maybe no real 'firing' at all. I mean, do you even need a 'real' projectile? You said it hits 100% chance right? Well, maybe don't even 'shoot' anything at all. Simply update the health information when the right time comes, and then pair it with some animations / objects / particles. The objects or particles created don't even have to register the hit. If it's just a muzzle flash for example, just do that while doing an Int Add or what ever that updates the health.

This is how I planned to do it, with some well timed animations, I also found a way to make a players unit attack an enemy in a predefined range.

But I also want to make a small computer-controlled enemy, and here I thought I will detect the player by using a sphere collider. But my problem is that if I give the enemy a sphere that is big enough to cover the range of the unit, then it blocks all my mouseclicks. Is there a way around this?

Thank you for the long and detailed answer I really aprechiate it.
« Last Edit: September 02, 2019, 07:37:29 AM by Nietzsche »

daniellogin

  • Full Member
  • ***
  • Posts: 215
Re: units attack each other / strategy game
« Reply #5 on: September 02, 2019, 08:52:18 AM »
There are ways to have objects included/excluded from actions. How exactly will depend on what kind of thing you want to be selective for, such as colliders (so an object is a ghost for some things but hits others), ray casting, and Mouse Pick.

You said click on. Are you using a Mouse Pick Event?


If you are using Mouse Pick Event; First create a new layer to put objects for mouse pick to ignore in. Then in the Mouse Pick Event action, change the Layer Mask to 1. This will now allow you to select a layer to ignore. Choose the one you just made. Mouse pick now works on all layers but that one. You can also flip the mask, which means you will pick what you only want it to work with. Last step is to change the layer for the objects you want to ignore, or if you flipped, want to only use.

If you aren't using Mouse Pick Event, what exactly are you trying to use? There will be a method the same or with a similar result to this for any action you use.

As for random chance, to hit; I haven't done any random things like that, but thinking up a system off the top of my head would go like this:

* Chance is between 0 and 100. It will be represented and interacted with by Ints
* Have a nerf Int ("x" in this example) which may start at 0 (if you start with 100% of being hit)
* When something reduces your chance to be hit, like you are behind cover or etc, add to your nerf (x) Int. So maybe you reduce your chance by 10% (which is 90% chance to be hit), so you add 10 to nerf
* Add for every thing that would reduce your chance of being hit, so this X int keeps building up.
* Conversely, you may also have modifiers which reduce this int. Like maybe you can do things to reduce your chance, but the enemy can do things to increase the chance. Just add or minus accordingly.
* Generate a random Int between 1 and 100 (this is "y" in this example)
* Int compare Y to X
* If Y is greater than X, you have been hit.
* If Y is smaller or equal to X, the shot has missed

For example, you reduce your chance by 10%, so your X is 10. That means you have 10 chances out of 100 to not be hit (1,2,3,4,5,6,7,8,9,10 as the int result), and 90 of being hit (11,12,13,..etc..,99,100). So if it produces 15 as your random int, you landed on part of the 90% chance of being hit.

Nietzsche

  • Playmaker Newbie
  • *
  • Posts: 26
Re: units attack each other / strategy game
« Reply #6 on: September 03, 2019, 10:55:05 AM »
wow, thanks again for the long answer, and this is pretty much how I ,figuered it out it would have to work.

I have not worked that much with arrays and layers so far, but I will try to learn more about it, since this seems to be the easiest way to implement all this.

Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: units attack each other / strategy game [SOLVED]
« Reply #7 on: September 03, 2019, 04:26:51 PM »
I am sorry for asking so much, but I am pretty new to the whole substance of programming. So please forgive me for asking a bit more questions.

Welcome. No need to apologize, that's the point of the forum :)

I am working on a small round based, hextile based game where units should be able to shoot at each other. I have just no idea how the shooting should be managed.

How can I make objects detect each other?
How can I make them shoot one annother? I would like to make a unit directly detect it's enemy and then hit it with a 100% chance. The projectile should also ignore anything in its path.

Sorry for the long post and thank you for your help.
If you know any good tutorials or guides on this subject please don't hesitate to tell me.

For problems like this, break down the steps that need to happen one by one.

Unity is always a 3D engine, but it has a suite of components (and code) that ignores the Z axis to make it effecient for 2D physics. You can tell these components by their naming that adds a "2D" at the end. These are colliders, triggers, rigidbody and rays and potentially more. PlayMaker also provides actions for both versions, which can be a source of confusion. If you want to use the ray, you also must use the correct version. Keep in mind that you can use the standard suite of 3D components and still make your game look 2D, and vice versa. But you need to know whether you need 3D space or 2D. When you know this, you can make the mechanic using Ray Cast or Ray Cast 2D, as explained above.

At that point your Game Design begins. Do the units have an orientation and only look forward? Do they have an all-around awareness and where they fire is determined by something else? How do you resolve a situation with multiple enemies in the range and view? You can for example shoot three raycast 2D in the three forward tile directions etc, or shoot one in the middle, then "turn" the ray -60° cast, and another time +120° to cover the forward "cone".

I would recommend to keep the mechanics in 2D, and prototype first (I simply use scenes for this and Unity now has built in 2D sprites to prototype, which you can color in the sprite renderer component). Believe me, once you set it up once, it's much faster to do it another time "for real" and you know what to keep in mind. You probably need to go through a few prototypes. I usually prototype mechanics separately, but I also prototype the graphics with and without (some) mechanics, to see how it works together.

In your case, I would invest in a solid hexagonal core, so that everything else from shooting to moving can make use of this, and you don't need to redo the math for every instance. For instance, an FSM that can return the tile that is seven tiles ahead. Whether you then shoot in this direction, or move there would come later.

Chances are simple. You can make them float based 0-1 range (0.5 = 50%), or more intuitive and game-like with ints, 0-100. Action: Int compare against the chance, if lower, success. E.g. chance 23%, int 23, Random Int, Int Compare 23 vs Random Int, if lower, success.


Nietzsche

  • Playmaker Newbie
  • *
  • Posts: 26
Re: units attack each other / strategy game [SOLVED]
« Reply #8 on: September 06, 2019, 04:18:50 PM »
thank you for the long answer, but do you maybe know a way to scan 360° with a raycast?

Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: units attack each other / strategy game [SOLVED]
« Reply #9 on: September 06, 2019, 05:09:33 PM »
thank you for the long answer, but do you maybe know a way to scan 360° with a raycast?

Rays aren’t suitable for covering large areas. You could cast an overlap circle, which returns colliders it overlapped with. There should be suitable actions, if not out of the box, then in the forum or ecosystem.

Nietzsche

  • Playmaker Newbie
  • *
  • Posts: 26
Re: units attack each other / strategy game [SOLVED]
« Reply #10 on: September 07, 2019, 03:04:44 PM »
so far I only found: "array list cast overlap sphere" and I am not really sure how to use it, since I have never worked with array lists so far.