Rotation around player is broken, does rotateAround not work correctly in 2D?
I'm making a Geometry Wars style game. I'm programming an enemy to move towards the player and start rotating around the player when the enemy is close enough. However, with my current code the enemy rotates in a triangle around a point in front of the player. Any idea what's causing it?
{
public Transform player;
public Rigidbody2D rb;
public float enemySpd = 3;
void Start()
{
//Sets player to the transform of the player
player = GameObject.FindGameObjectWithTag("Player").transform;
}
void FixedUpdate()
{
FollowPlayer();
}
void FollowPlayer()
{
//Moves the enemy towards the player
transform.right = player.position - transform.position;
//Gets the distance between the enemy and the player
float dist = Vector2.Distance(player.position, transform.position);
//Sets zAxis to the z axis for the enemy to rotate around
Vector3 zAxis = new Vector3(0,0,1);
//Sets vec to an X axis for the directions of the enemy to face
Vector2 vec = new Vector2(1, 0);
//If the enemy is far enough away have him move toward the player
if (dist > 3)
{
//Moves the enemy forward
rb.velocity = transform.TransformDirection(vec * enemySpd);
}
//If the enemy is close enough stop moving forward and rotate around the player
else
{
//Rotates around the Z axis of the player
transform.RotateAround(player.position, zAxis, 1);
}
}
}
Answer by ArminAhmadi · Jan 30, 2019 at 07:31 AM
Hello. I changed the code a little. I hope this helps :
private Transform player;
private Rigidbody2D rb;
private void Start()
{
// Get components
player = GameObject.FindGameObjectWithTag("Player").transform;
rb = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
FollowPlayer();
}
private void FollowPlayer()
{
if (player)
{
// Moves the enemy towards the player
transform.right = player.position - transform.position;
// Gets the distance between the enemy and the player
float dist = Vector2.Distance(player.position, transform.position);
// Sets zAxis to the z axis for the enemy to rotate around
Vector3 zAxis = new Vector3(0, 0, 1);
// Sets vec to an X axis for the directions of the enemy to face
Vector2 vec = new Vector2(1, 0);
if (dist > 3)
{
// If the enemy is far enough away have him move toward the player
rb.bodyType = RigidbodyType2D.Dynamic;
rb.velocity = transform.TransformDirection(vec * moveSpeed);
}
else
{
// If the enemy is close enough stop moving forward and rotate around the player
rb.bodyType = RigidbodyType2D.Static;
transform.RotateAround(player.position, zAxis, 1);
}
}
}
What an absolute mad lad. Thank you so much for helping me with this - I see I need to use rigidbody more. Why does a static rigidbody fix this tho?
Your answer
Follow this Question
Related Questions
Simple Rotate Script won't work! 2 Answers
Horizontal Input showing random decimal when it should be 0 0 Answers
Google play services won't start. 0 Answers
Unity - Problems with Bone Rotations within constraints 0 Answers
how do i rotate an object? C# 1 Answer