Restricting Player Movement to go towards a selected target.

Hello Everyone,

How would you go about restricting the player character to only move forward towards a selected target when Input.GetAxis(“Horizontal”) and not over time as it would be with an Enemy?

I see many tutorials on path finding but not on character controlled objects like in the case I want to do.

Thank you in advance and sorry for the newbie question.

How do you want to restrict player movement? Like literally, if they’re trying to move away from it, just don’t move?

If so, you can do something like:

 if (Vector3.Angle( (enemy.position - player.position), desiredMovementVector) <= 90f) {
//move
}
else {
//don't move
}

Hi StarManta,

Basically if you are going forward you’re going towards a target instead of free X,Z.

Like Zelda’s Z-targeting?

Like Tales of games, where you select a target enemy and run towards it.

I got it working, may be not the best way but here is what I did:

if (Input.GetAxis("Horizontal") > 0.1)
        {
            transform.position = Vector3.Lerp(transform.position, target.transform.position, speed * Time.deltaTime);
        }
        else
        {

        }

Use Vector3.MoveTowards rather than Lerp.