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.
- Where the heck do i even start to make a REAL grid (not just an imaginary mathematical one) that things can snap to?
- Is it possible to make this grid without using the projector?
- After making this grid how do you snap the objects to the center of those grid squares?
- 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:
- What is the best way to approach a similar collision detection?
- 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:
- When spacebar is pressed, start raycasting and instantiate the temporary object at the rays hit position.
- Lock the temporary object to the hit position with modelPosition = hit.point;.
- 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; - 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))
- OnTriggerEnter with certain tagged objects turn red, OnExit turn green
Thanks again!