Flipping the player by rotation

Im trying to get an airplane to flip when it turns instead of fly upside down, but all the tuts i see on youtube are just x and y axis examples

im using mouse or the touch screen.

i just dont know how to see which direction the object is moving

could do with some help here is the code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    Rigidbody2D rb;
    public float moveSpeed;
    public float rotateAmount;
    float rot;
    bool facingRight = true;

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
    }
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
           
            if (mousePos.x < 0)
            {
                rot = rotateAmount;
            }
            else
            {
                rot = -rotateAmount;
            }

            transform.Rotate(0, 0, rot);
        }

        //Flip plane in right direction
        if (rot < 0 && facingRight)
        {
            Flip();
        }

        else if (rot > 180 && !facingRight)
        {
            Flip();
        }

        Debug.Log(rot);
    }

    private void FixedUpdate()
    {
        rb.velocity = transform.right * moveSpeed;
    }

    void Flip ()
    {
        facingRight = !facingRight;
        transform.Rotate(0f, 180f, 0f);
    }
}

I love these sortsa games… I’ve played quite a few myself, such as Sopwith and DAWGFIGHT.

This is a bit tricky to get right… I think your problem is the line 64 flip interfering with your actual rotation.

I changed your script around to do a FlipY on the sprite, based on looking left / right. And it bases left/right on whether the transform.right.x value is negative or positive.

There was also a problem with your camera cast into world: the Z component from Input.mousePosition is zero, which means "what is the world position at +0 Z from the camera), which will always be the camera’s actual position.

So I added a +1 to the Z before using it, so left side turns plane counter clockwise, right side clockwise.

You might want to consider NOT casting by the camera and instead comparing the X position of the mouse to the term Screen.width / 2 because it will work even if your camera moves around following the plane.

I renamed your script PlaneController so you can try them side by side if you like:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlaneController : MonoBehaviour
{
    Rigidbody2D rb;
    [Tooltip("World units per second.")]
    public float moveSpeed;
    [Tooltip("Degrees per second.")]
    public float rotateAmount;
    float rot;

    SpriteRenderer spriteRenderer;

    private void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        spriteRenderer = GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            Vector3 mousePos = Input.mousePosition;
            mousePos.z = 1;    // gotta cast a little bit into the scene!

            mousePos = Camera.main.ScreenToWorldPoint(mousePos);

            if (mousePos.x < 0)
            {
                rot = rotateAmount;
            }
            else
            {
                rot = -rotateAmount;
            }

            transform.Rotate(0, 0, rot * Time.deltaTime);
        }

        SetFlipY();

        Debug.Log(rot);
    }

    private void FixedUpdate()
    {
        rb.velocity = transform.right * moveSpeed;
    }

    void SetFlipY()
    {
        // I'm not going to base it off of rot but rather off of the
        // sign of the x component of the transform.right vector.
        bool flipy = transform.right.x < 0;
        spriteRenderer.flipY = flipy;
    }
}

PS - I took the liberty of adding in the Time.deltaTime term when rotating, which will preserve behavior regardless of frame rate on different platforms. You will have to increase your rotation to be “degrees per second” to match your world units per second using rigidbody velocity.

hey dude thanks for replying

got no control man, my plane just flies off the screen lol

Did you set your turn to nonzero?

yeah and speed too

i like those types of games too!!!

Did you set the turn to something like 90? that will take 2 seconds to turn 180 degrees.

Also, what does your Debug.Log(rot) say?

when i move this line transform.Rotate(0, 0, rot * Time.deltaTime);

out f here

  • if (Input.GetMouseButton(0))
  • {
  • Vector3 mousePos = Input.mousePosition;
  • mousePos.z = 1; // gotta cast a little bit into the scene!
    • mousePos = Camera.main.ScreenToWorldPoint(mousePos);
    • if (mousePos.x < 0)
  • {
  • rot = rotateAmount;
  • }
  • else
  • {
  • rot = -rotateAmount;
  • }
    • transform.Rotate(0, 0, rot * Time.deltaTime);
  • }

so after the if, it works!

but not properly

Got it working perfectly man, you are a genius!!! I really need to watch tutorials on Vector etc, i dont get it in this state!!!
Super stuff again, thanks!!! You are a hero!!!

1 Like

Now go make some enemies, make some gunning, and blast 'em out of the sky! We’re all counting on you Red Baron!

Hey!!! Not so fast!!! I gotta study that magic you wielded there! i hate just copying stuff,i need to know whats going on.

thanks again man!

I played DAWGFIGHT and SOPWITH a ton back in the day, and the way the controls-flip worked was that as long as you were looping around, it would NEVER flip the plane over. It would only flip when you let go of the key, and then it would flip so the tail was upright.

You can trivially do this in the script above by changing this line:

SetFlipY();

to instead be:

else
{
   SetFlipY();
}

Assuming you did not move it away from the end of the closing brace of the if() clause.

I like that mode better because it looks like you are really loop-de-looping.

Wow!!! thanks Mr Kurt!!! Have you done a similar project? How do you have solutions so fast? I would really like to learn the thought processes when solving stuff like this, its very very interesting.

Works like a dream, you are really gifted!!!