Author Topic: Ability to adjust the in-path speed of a unit  (Read 1493 times)

incursio

  • Hero Member
  • *****
  • Posts: 569
    • View Profile
Ability to adjust the in-path speed of a unit
« on: October 26, 2018, 10:08:07 PM »
In my game, as in games by others, I'm sure, provinces have certain geographical features that could serve to slow down (or even speed up) travel by units. Things like infrastructure level (roads/rails), swamps, marshes, mountains, deserts, etc. can all play a role.  In reality, military units never move at a constant speed, and there should/could be developer-implemented penalties for trying to move across certain provinces.

It would be nice to be able to somehow affect the speed of a unit that is currently moving, depending on attributes of the province, for example.  Hope that makes sense!

What I have been doing is using a "speed" attribute for each type of unit, and using that to seed the MoveTo duration. It works, but it isn't perfect. A true "base unit speed" would be nice, and could be modified by province attributes. Pseudo-code here, but it might illustrate the concept a bit:

Code

pollMethod() {

  if(this.currentProvince().attrib["swamp"] == true) {
    this.speed -= 3;
  }

}

Cheers.

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 7262
    • View Profile
Re: Ability to adjust the in-path speed of a unit
« Reply #1 on: October 27, 2018, 12:34:24 PM »
This is a good idea for RTS games - will think about this.

Currently you can assign costs to regions, provinces, countries, cells or even specific map positions but that will only influence the cost computation when issuing a FindRoute call. So if you have say, 15 movement points and you can cast a path between two points with those 15 points, but then the unit will just move linearly between them like shown in this video from a Path-finding demo scene: https://www.youtube.com/watch?v=HOn3QyzG4vM


kalelovil

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Ability to adjust the in-path speed of a unit
« Reply #2 on: February 21, 2020, 02:23:08 AM »
My apologies if it is against forum etiquette to reply to an inactive thread.

I would also find such a feature very useful.
A variable like the existing WMSK.VGOGlobalSpeed, but individual to each unit instance instead of global.

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 7262
    • View Profile
Re: Ability to adjust the in-path speed of a unit
« Reply #3 on: February 21, 2020, 09:33:38 AM »
Hi!

VGOGlobalSpeed option just multiplied to the GameObjectAnimator duration property.
Instead of adding a new "speed" option to each GameObjectAnimator, maybe you could just modify the duration?

kalelovil

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Ability to adjust the in-path speed of a unit
« Reply #4 on: February 23, 2020, 11:45:02 PM »
Hi!

VGOGlobalSpeed option just multiplied to the GameObjectAnimator duration property.
Instead of adding a new "speed" option to each GameObjectAnimator, maybe you could just modify the duration?

The problem I have run into is that VGOGlobalSpeed does not seem to affect movements in progress, only new movements.

I can issue a new GoAnimator.MoveTo call when terrain and non-terrain regions are entered, reflecting the new speed, but this runs into two smaller problems.

<s>1. Each time you pathfind the unit initially moves very slowly and not necessarily in the right direction, I guess to reach the closest node in the pathfinding cost array.
2. The new path determined may not be quite the same as that section of the previous path. The player would see the line marker path change.
(Although I'll try now re-using the remaining subset of the previous route as the parameter for the "MoveTo (List<Vector2> route" function)</s>

Edit: Re-using the previous route has avoided those two problems. In case anyone else is doing this the code I am using after setting new unit speed looks like:
<code>
        int index = Mathf.CeilToInt((goAnimator.Route.Count - 1) * goAnimator.Progress);
        List<Vector2> route = goAnimator.Route.GetRange(index, goAnimator.Route.Count - index);
        route.Insert(0, GoAnimator.currentMap2DLocation);
        ...
        GoAnimator.MoveTo(route, 1e4f / Speed, DURATION_TYPE.MapLap);
</code>
« Last Edit: February 24, 2020, 12:56:32 AM by kalelovil »

Kronnect

  • Administrator
  • Hero Member
  • *****
  • Posts: 7262
    • View Profile
Re: Ability to adjust the in-path speed of a unit
« Reply #5 on: February 28, 2020, 07:05:25 PM »
Latest update includes a new method in GameObjectAnimator class called ChangeDuration which can be used to add/reduce global duration of the path while the unit is moving.

For example, calling:

Code
tank.ChangeDuration(10f);

will make the path take additional 10 seconds to complete (effectively reducing the unit speed).

To accelerate unit speed, just pass a negative number:

Code
tank.ChangeDuration(-10f);