Buat musuh ikuti dan putar menuju target dalam persatuan
///We shall make the enemy follow the target and then we will make the enemy flip when the target set comes behind the enemy along the x axis
///You can use this script to make an enemy follow the player or to make an object flip towards an object
//For this script to work, you must assign a game object in Unity in the followTarget field.
//Declare Variables
public GameObject followTarget;
public bool _facingRight = false;
void Update()
{
//Makes the game object attached to the script move towards the gameobject set in the followTarget
transform.position = Vector2.MoveTowards(transform.position, followTarget.transform.position, _moveSpeed* Time.deltaTime);
//Compares the target and the object transform and flips in the direction towards the position that the setTarget is currently at.
if (followTarget.transform.position.x < gameObject.transform.position.x && _facingRight)
{
flip();
}
if (followTarget.transform.position.x > gameObject.transform.position.x && !_facingRight)
{
flip();
}
}
private void flip()
{
_facingRight = !_facingRight;
//assigns a the scale component to a variable temporarily
Vector3 tmpScale = gameObject.transform.localScale;
tmpScale.x *= -1;
gameObject.transform.localScale = tmpScale;
}
Eric the Grey