insira seu código aqui
public class EnemyShip : MonoBehaviour { private Vector3 randomPosition; private Vector3 direction;
private Rigidbody enemyshipRigidbody;
public GameObject Player;
[HideInInspector]
public float PlayerDistance;
public float Speed = 1;
public float EnemyFarDistance = 50;
private float wanderingTimer;
private float randomPositionsTimer = 4;
public float Step = 6;
bool isAlive = true;
public int EnemyshipLife = 100;
// Start is called before the first frame update
void Start()
{
enemyshipRigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
PlayerDistance = Vector3.Distance(enemyshipRigidbody.transform.position, Player.transform.position);
}
// Ainda não tem animator para ataque das naves inimigas, assim que tiver denominar "Attack", ou mudar o nome nas linhas abaixo em comentario.
void FixedUpdate()
{
float distance = Vector3.Distance(transform.position, Player.transform.position);
Quaternion newRotation = Quaternion.LookRotation(direction);
enemyshipRigidbody.MoveRotation(newRotation);
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Step * Time.deltaTime);
if (distance > 100)
{
Wandering();
}
else if (distance < 100)
{
Chasing();
//GetComponent<Animator>().SetBool("Attack", false);
}
else if (distance <= EnemyFarDistance)
{
}
/*else
{
GetComponent<Animator>().SetBool("Attack", true);
}*/
}
void Chasing()
{
direction = Player.transform.position - transform.position;
enemyshipRigidbody.MovePosition
(enemyshipRigidbody.position + direction.normalized * Speed * Time.deltaTime);
}
void Wandering()
{
wanderingTimer -= Time.deltaTime;
if (wanderingTimer <= 0)
{
randomPosition = RandomizePosition();
wanderingTimer += randomPositionsTimer;
}
bool isCloseEnough = Vector3.Distance(transform.position, randomPosition) <= 5;
if (isCloseEnough == false)
{
direction = randomPosition - transform.position;
enemyshipRigidbody.MovePosition
(enemyshipRigidbody.position + direction.normalized * Speed * Time.deltaTime);
}
}
Vector3 RandomizePosition()
{
Vector3 position = Random.insideUnitSphere * 100;
position += transform.position;
position.y = transform.position.y;
return position;
}
}