Enemy Rotation to Player
I am beginning to despair.
I want the opponent to turn towards the player ship. Depending on where the player is, the right side of the cannon or the left side should be directed towards the player. So far I'm getting there, but I don't know how to stop the rotation when the opponent's side is facing the player.
My Code
if (Vector3.Dot(transform.right, Cube.transform.position) > 0) { transform.rotation = Quaternion.Euler(new Vector3(transform.position.x, rotatey, transform.position.z)); rotatey -= 1; //Stop it when Player is in 90degrees direction to the right side } else { transform.rotation = Quaternion.Euler(new Vector3(transform.position.x, rotatex, transform.position.z)); rotatex += 1; //Stop it when Player is in 90degrees direction to the left side }
Answer by FlorianWpW · Feb 04 at 10:04 AM
I Updatet the Code but it doesnt work fine
Vector3 directiontoface = (hitCollider.transform.position - transform.position).normalized; Debug.DrawRay(transform.position, directiontoface, Color.red);
if (Vector3.Dot(transform.right, hitCollider.transform.position) > 0.1F)
{
Debug.DrawRay(transform.position, transform.right, Color.green);
Vector3 posa = new Vector3(0, 0, directiontoface.z);
Vector3 posb = new Vector3(0, 0, transform.right.z);
if (Vector3.Distance(posa, posb) > 0F)
{
transform.rotation = Quaternion.Euler(new Vector3(transform.position.x, rotatex, transform.position.z));
rotatex -= 1;
}
//Debug.Log("Angel face " + directiontoface);
//Debug.Log("Angel right " + transform.right);
//transform.rotation = Quaternion.Euler(new Vector3(transform.position.x, rotatey, transform.position.z));
//rotatey -= 1;
}
else if (Vector3.Dot(-transform.right, hitCollider.transform.position) > 0.1F)
{
Debug.DrawRay(transform.position, -transform.right, Color.green);
Vector3 posa = new Vector3(0, 0, directiontoface.z);
Vector3 posb = new Vector3(0, 0, -transform.right.z);
if (Vector3.Distance(posa, posb) > 0F)
{
transform.rotation = Quaternion.Euler(new Vector3(transform.position.x, rotatey, transform.position.z));
rotatey += 1;
}
}
Your answer
Follow this Question
Related Questions
How can I organize menus as a tree hierarchy? 0 Answers
Removing and replacing a Instantiated grid object 0 Answers
is there a limit to the number of attributes that can be assigned a single unit? 0 Answers
Select Objects ingame and show information 0 Answers
Deactivating gameobject when other game objects are disabled 0 Answers