RTS help...

So we are creating a real time strategy type game and i’ve got a few pressing questions. I am currently at a loss on an efficient way of doing unit placement. As it stands right now, i have a camera with look and movement behaviors of a real time strategy game (45ish degree angle, mouse to side scrolling, and zoom, etc.) and a terrain to place objects on. I can also place units (my method works, but the point of this post is that there has to be a better way lol).

Unit placement…
During live gameplay the mouse movement is smooth. Then when the spacebar is pressed a temporary transparent game object is instantiated at the position of a raycast hit and the mouse goes from a smooth behavior to a grid snapping behavior (build mode) using:

var gridSpacing : float = 10.0;
myObject.transform.position.x = Mathf.Round(myObject.transform.position.x / gridSpacing) * gridSpacing;
myObject.transform.position.z = Mathf.Round(myObject.transform.position.z / gridSpacing) * gridSpacing;

Anyways, you can “move snap” the temp object around the screen and then when you left click, a permanent object is created and the temporary object is deleted.

  1. Where the heck do i even start to make a REAL grid (not just an imaginary mathematical one) that things can snap to?
  2. Is it possible to make this grid without using the projector?
  3. After making this grid how do you snap the objects to the center of those grid squares?
  4. What if a unit takes up multiple squares, how does snapping work then?

My second question revolves around the collision detection of my temporary object. Pretty much i created 9 separate planes and snapped them together. The point is that you can place the object if all the squares are green, if any are red, you cannot place your unit there. This works fine for a flat terrain but if hills start to come in i dont want my collider object poking through the sides of hills. see image and basic script run throughs below:

  1. What is the best way to approach a similar collision detection?
  2. What is the standard method in RTS’s?

In all, my ideal situation would be for when you press the spacebar and enter build mode a grid appears over an uneven terrain and i can “snap” move temp objects around those grid squares, then click to place a permanent object. Furthermore, I would want it to be able to turn a grid square green when it can be built upon or red when that square is unable to be built upon (caused by an already existing object or too steep of an incline).

I can live without a multibox unit placement collider i suppose but I would REALLY like to get the grid aspect down.

Sorry for the long winded post, i have just been at such a loss. There has got to be a better way than how im doing it. Thanks all, I really appreciate any guidance, tips, or help you can provide :). -Aaron

http://img218.imageshack.us/img218/1618/collider.jpg

Instantiate Script:

  1. When spacebar is pressed, start raycasting and instantiate the temporary object at the rays hit position.
  2. Lock the temporary object to the hit position with modelPosition = hit.point;.
  3. Make it so the temporary object fakes being on a grid by using:
    -var gridSpacing : float = 10.0;
    -myObject.transform.position.x = Mathf.Round(myObject.transform.position.x / gridSpacing) * gridSpacing;
    -myObject.transform.position.z = Mathf.Round(myObject.transform.position.z / gridSpacing) * gridSpacing;
  4. On Input.GetMouseButtonDown(0) make the permanent object, delete the temporary object, turn off raycasting, turn off fake grid snapping.

Proximity Detection Script (assigned to the each 1 of the 9 squares in the picture above (the ones that turn colors))

  1. OnTriggerEnter with certain tagged objects turn red, OnExit turn green

Thanks again!

Nice post :slight_smile:

There’s a lot there, so I’ll just comment on a couple of things:

Regarding the visual aspect, there’ve been some discussions here in the past regarding drawing lines and other geometry on terrain surfaces (I can’t say for sure, but a forum search for ‘terrain grid’ might turn something up).

Would the grid still be a regular grid when viewed orthographically from above? Or do you want to create an irregular grid of some sort?

Probably similar to the way you’re doing it now; you’ll just different information to work with. That’s assuming, of course, that the grid is still a regular grid.

When the unit is (virtually) moved to a new square, can you just check all the squares that it would occupy if placed there, and then simply keep it at it’s current position if there’s a collision?

Hey Jesse,

Thanks so much for the reply :slight_smile:

Yes, the grid would be a regular grid when viewed orthographically. In the sense, that is, that each square would have the same dimensions.

Well if the way i am doing it is the correct way, then maybe projectors is the best way to go if im using the math method to snap move around?

It would just be really cool to make grids on the fly though so that i don’t have to make my map 100% perfect in size by using the imaginary mathematical grid.

Thanks again Jesse. Does anyone else have any input or experience with this sort of thing? Any input is good input lol. Thanks to all in advance. :slight_smile: -Aaron