RTS Style building help

I’m making an RTS game and I have a GUI that I click to buy a building and place it. I’m trying to figure out how I can make the building place under the ground then slowly come up while at the same time taking a little amount of resources until the building is built.

For some reason when trying to place it under the ground this code wont work:

	Vector3 newpos = new Vector3 (currentBuilding.transform.localPosition.x, -currentBuilding.transform.localPosition.y ,currentBuilding.transform.localPosition.z);

				

					currentBuilding.transform.localPosition = newpos;

As for taking the resources that’s where I’m stuck, I know how to make it move up and take resources away at the same time using a while loop in an IEnumerator but how could I : Make it move up by a number of seconds, take a small amount of resources each second AND make the small amount it is taking equal the total price of the whole building by the time its done the number of seconds it took to build? Thanks any feedback is appreciated!

Seems to me you should replace

-currentBuilding.transform.localPosition.y

by -YourBuildingHeight. As your localPosition.y is probably = 0. So -localPosition.y is still =0.

As for your ressources:

Each second:

money -= totalBuildingPrice / totalBuildTime;
anyOtherResource -= totalRessourceForThisBuilding / totalBuildTime;
... Same for all ressources this building costs

It’s some pretty basic math for all of these things. Simply put you want to do the following:

  1. Place the structure where it should be when completed
  2. Determine the incremental cost and delta position of the structure (where it should start).
  3. Start a loop for the desired period of time
  4. Lerp between the start and end position of the structure
  5. Reduce the resources by the incremental value

If you’re visibly subtracting the cost over time then you’ll need to also keep track of the funds available; otherwise it’ll be possible to start building two things at the same time and end up in the negative.

To perform your loop I’d recommend either writing a helper function that’ll do something over time, or using a library like DOTween. An example of a function to do arbitrary things over time would look like this:

public static IEnumerator DoSomething(float duration, Action<float> callback) {
  float elapsed = 0;
  while ((elapsed += Time.deltaTime) < duration) {
    callback(elapsed / duration);
  }
}

void Example() {
  const float DURATION = 15;
  const int TOTAL_COST = 250;
  var incrementalCost = TOTAL_COST / DURATION;
  var targetPosition = MagicStructureProperty.transform.position;
  var startPosition = targetPosition - Vector3.up * 10;
  StartCoroutine(DoSomething(DURATION, t => {
    Wallet.balance -= incrementalCost;
    MagicStructureProperty.transform.position = Vector3.Lerp(startPosition, targetPosition, t);
  }));
}

Definitely not the most elegant solution, but that’s how it’s done at a very basic level. I just want to set you in the right direction. Enjoy!