public float speed;
void Update ()
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
transform.Rotate(0, 0, -6);
}
void OnCollisionEnter2D(Collision2D col)
{
if(col.collider.tag == "FinishPatrolEnemyRight"
{
transform.eulerAngles = new Vector3(0, -180, transform.eulerAngles.z);
}
if(col.collider.tag == "FinishPatrolEnemyLeft"
{
transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z);
}
}
I am trying to create an object with a circle collider that is patrolling. The problem is that, when i rotate the object, it’s affecting the direction.
You might want to add Space.World for your transform.Translate and change your transform.rotate to transform.rotation.
transform.Translate(Vector2.right * speed * Time.deltaTime, Space.World); //Movement
transform.rotation = Quaternion.Euler(0, 0, -6); //Rotation
So it will look like this:
public float speed;
void Update ()
{
transform.Translate(Vector2.right * speed * Time.deltaTime, Space.World);
transform.rotation = Quaternion.Euler(0, 0, -6);
}
void OnCollisionEnter2D(Collision2D col)
{
if(col.collider.tag == "FinishPatrolEnemyRight"
{
transform.eulerAngles = new Vector3(0, -180, transform.eulerAngles.z);
}
if(col.collider.tag == "FinishPatrolEnemyLeft"
{
transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z);
}
}
If this helps you well goodluck with your project!