Mouse movement on a budget

Hi

So i need some advice.

I want to built a movememt system for a Tactical Turn Based RPG.

When out of combat.
1)Mouse click to move
2) Path find around objects

When combat is triggred

  1. movent has to be restricted to a variable as a move action. So if you can move 60 feet. You move 60 and then you are out

I have the raycast mouse move and Navemesh built.

How would you suggest i do this. Also a suggestion on the visual aids would be appreaciated

So, what do you want advice on? Seems like you already have the parts you need for it.

1 Like

You got started good, you raycast against the terrain to find a position and then you use the navmesh to move to it.

As for movement distance, I think you need “distance covered”, not “distance in a crow line”.

Every frame your character moves you store the previous location and subtract the distance of your current location to the previous location from your movement points. Also don’t forget to save the current position as previous one for the next frame. If you want Heroes of Might and Magic like movement points, you can round these to integer.

private Vector3 previousPosition;
private float movementPoints;

...

void Update()
{
   var currentPosition = transform.position;
   movementPoints -= Vector3.Distance(currentPosition, previousPosition);
 
   previousPosition = transform.position;
}
1 Like

There are some things you can do to display how far the player can go. I imagine it would be too expensive to calculate every possible position the player could move to. You definitely would need to have some concessions in order to be able to do it.

Some ways off of the top of my head which might work for you.

You could base your flood-fill search off of a grid, in order to limit how many points you have to search. This would definitely bring a bit of inaccuracy to your edges based on how thin your grid is, but would allow you to highlight the entire area you could move, kind of like in X-COM.

Another way is you could draw the best path to where your mouse cursor is, and “cut it off” when it reaches max distance. That way, the player could explore where the max distance is themselves, but obviously gives less information to the player.

1 Like

To measure distance you could do something like

NavMeshPath temp_nav_path = new NavMeshPath();
NavMesh.CalculatePath( start,  gotopos, NavMesh.AllAreas, temp_nav_path );

float lng = 0.0f;      
        if ( ( temp_nav_path.status != NavMeshPathStatus.PathInvalid ) )
        {
            for ( int i=1; i<temp_nav_path.corners.Length; ++i )
            {
                lng += Vector3.Distance( temp_nav_path.corners[i-1], temp_nav_path.corners[i] );
            }
        }
1 Like

This looks like a very good idea.

I will dig into this in the next few days thanks

Sure, Sounds like I am thinking the same as you. On the move distance visual aid concept. I would just display a line in green to a point on screen and that ray would turn red if the distance is greater than your move range.

So there is no need for a grid ?

Not if you were just going to draw the line. The grid was just an efficient idea to fill up the whole area.

Cool

The when and where of the why of using a grid is not well understood on my side.

If you’ve ever played X-COM or Fire Emblem, it’s the way they show the entire area the character can move to.

Like this, they use some kind of flood-fill method to find each square the character can move to, and shows it. The problem with your game, is that it’s not tile based, so it would be extremely expensive to calculate every position the player could move to. One way we can reduce that, is to only poll a limited amount of positions, and only check every position that’s 0.1 distance apart, kind of treating it like a grid.