- Home /
How can I rotate the object without affecting the direction ?
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.
Answer by RexCodeOfficial · Dec 24, 2020 at 11:05 AM
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220612231619im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Arm Rotation toward Cursor Script Help 1 Answer
2D sprite animation issue 0 Answers
2D Projectile Not Firing Based on Rotation 1 Answer
object rotates toward mouse? 2D Top Gameplay 6 Answers
Angle to Rotation 2 Answers