Basketball Game Defense

Well I have a player let’s call him attacker. And I want to put an opponent in front of him and in front of the basket so to “defend”. I use a script that finds the distance but if i move my attacker right left the defender does not follow.

The script is that:

var attacker:Transform;
var speed:float = 1.0;

function Update () {

	var lineToAttacker = attacker.transform.position - transform.position; 
	var distanceToAttacker = Vector3.Distance(attacker.position, transform.position);
		
	transform.Translate(lineToAttacker * Time.deltaTime * speed);
	
}

Well what i want to achieve is that the defender always follows the attacker and it is in front of him and in front of the basket. Is it doable somehow?

Of course it’s doable - how could it not be? :wink:

I think you’ll probably want to use a different approach than you’re using though. Programming ‘real’ defensive behavior could get pretty complicated, but here are some suggestions as to how to get started.

At it’s simplest, what you’re essentially looking for is the ‘interpose’ steering behavior as (briefly) described here.

Here’s another way to think about it. You have a line segment connecting the player and the goal. Ideally, the defender wants to be standing somewhere along this line segment. A simple solution would be always to move the defender towards a point on that segment that’s fairly close to the player. A slightly more sophisticated implementation might take other factors into account as well, such as the defender’s distance from the ideal position, and the player’s distance from the goal.