Game Development Reference
In-Depth Information
drone = transform.position;
target = playerTransform.position + Vector3(0, 0.6, 0);
distance = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(drone, target, distance);
}
}
This code breaks down as follows:
(1) public var playerTransform : Transform;
public var speed : float = 3.0f;
1.
Declare the playerTransform Transform public-type variable to hold the
reference to the player character's transform for targeting, and the speed
float-type variable to make the speed of the drone adjustable from the
Inspector during playtesting.
(2) private var drone : Vector3;
private var target : Vector3;
private var distance : float;
2.
Declare the private variables that will hold the values required by the
MoveTowards() function: the position of the drone, the position of the player
character, and the distance between the two.
(3) if (playerTransform.position.z > 128 && playerTransform.position.z < 145)
3.
This conditional checks to see if the player character's z Transform position is
greater than 128, in which case the player character has entered the drone's
zone. It also checks to see if the player character's z Transform position
is less than 145, meaning he hasn't exited the zone on the far side. The
&& logic operator means that if both of these conditions are true, then the
player character has entered the zone and has not exited it, and the player
character is within the enemy drone's zone.
(4) transform.LookAt(playerTransform);
4.
The transform.LookAt function rotates the drone's transform so it's forward
vector points toward the player character by taking the player character's
Transform as its argument.
(5) drone = transform.position;
target = playerTransform.position + Vector3(0, 0.6, 0);
distance = speed * Time.deltaTime;
Search WWH ::




Custom Search