2
\$\begingroup\$

I am using the Unity Engine and coding in C#.

I have a sprite as a parent with a main camera as a child so that the main camera follows the sprite. However, when pressing 'A' and 'D' to move the camera 'flips 180 degrees' due to me using transform.eulerangles is there anyway that the camera can be excluded from this 'flip'?

void Movement () {

anim.SetFloat ("moveSpeed", Mathf.Abs (Input.GetAxisRaw ("Horizontal")));

if (Input.GetAxisRaw ("Horizontal") > 0) {
  transform.Translate (Vector3.right * moveSpeed * Time.deltaTime);
  transform.eulerAngles = new Vector2(0,0);
}

if (Input.GetAxisRaw ("Horizontal") < 0) {
  transform.Translate (Vector3.right * moveSpeed * Time.deltaTime);
  transform.eulerAngles = new Vector2(0,180);
}
}
\$\endgroup\$
0
\$\begingroup\$

Hold a reference to your Camera, then set its localEurlerAngles to Vector3.zero.

Camera yourCamera;

void Start()
{
    yourCamera = GetComponentInChildren<Camera>();
}

void Movement () {

    anim.SetFloat ("moveSpeed", Mathf.Abs (Input.GetAxisRaw ("Horizontal")));

    if (Input.GetAxisRaw ("Horizontal") > 0) {
      transform.Translate (Vector3.right * moveSpeed * Time.deltaTime);
      transform.eulerAngles = new Vector2(0,0);
      yourCamera.transform.localEurlerAngles = Vector3.zero; 

    }

    if (Input.GetAxisRaw ("Horizontal") < 0) {
      transform.Translate (Vector3.right * moveSpeed * Time.deltaTime);
      transform.eulerAngles = new Vector2(0,180);
      yourCamera.transform.localEurlerAngles = Vector3.zero; 
    }
}
\$\endgroup\$
2
\$\begingroup\$

Rather than rotating a sprite, I would suggest you try multiplying the sprite's x scale by -1. This will flip the sprite without having to rotate anything.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.