Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Flipping the player by rotation

Discussion in 'Scripting' started by epochplus5, Oct 6, 2020.

  1. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     Rigidbody2D rb;
    8.     public float moveSpeed;
    9.     public float rotateAmount;
    10.     float rot;
    11.     bool facingRight = true;
    12.  
    13.     private void Awake()
    14.     {
    15.         rb = GetComponent<Rigidbody2D>();
    16.     }
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.        
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         if (Input.GetMouseButton(0))
    27.         {
    28.             Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    29.            
    30.             if (mousePos.x < 0)
    31.             {
    32.                 rot = rotateAmount;
    33.             }
    34.             else
    35.             {
    36.                 rot = -rotateAmount;
    37.             }
    38.  
    39.             transform.Rotate(0, 0, rot);
    40.         }
    41.  
    42.         //Flip plane in right direction
    43.         if (rot < 0 && facingRight)
    44.         {
    45.             Flip();
    46.         }
    47.  
    48.         else if (rot > 180 && !facingRight)
    49.         {
    50.             Flip();
    51.         }
    52.  
    53.         Debug.Log(rot);
    54.     }
    55.  
    56.     private void FixedUpdate()
    57.     {
    58.         rb.velocity = transform.right * moveSpeed;
    59.     }
    60.  
    61.     void Flip ()
    62.     {
    63.         facingRight = !facingRight;
    64.         transform.Rotate(0f, 180f, 0f);
    65.     }
    66. }
    67.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    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:

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlaneController : MonoBehaviour
    6. {
    7.     Rigidbody2D rb;
    8.     [Tooltip("World units per second.")]
    9.     public float moveSpeed;
    10.     [Tooltip("Degrees per second.")]
    11.     public float rotateAmount;
    12.     float rot;
    13.  
    14.     SpriteRenderer spriteRenderer;
    15.  
    16.     private void Awake()
    17.     {
    18.         rb = GetComponent<Rigidbody2D>();
    19.         spriteRenderer = GetComponent<SpriteRenderer>();
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         if (Input.GetMouseButton(0))
    26.         {
    27.             Vector3 mousePos = Input.mousePosition;
    28.             mousePos.z = 1;    // gotta cast a little bit into the scene!
    29.  
    30.             mousePos = Camera.main.ScreenToWorldPoint(mousePos);
    31.  
    32.             if (mousePos.x < 0)
    33.             {
    34.                 rot = rotateAmount;
    35.             }
    36.             else
    37.             {
    38.                 rot = -rotateAmount;
    39.             }
    40.  
    41.             transform.Rotate(0, 0, rot * Time.deltaTime);
    42.         }
    43.  
    44.         SetFlipY();
    45.  
    46.         Debug.Log(rot);
    47.     }
    48.  
    49.     private void FixedUpdate()
    50.     {
    51.         rb.velocity = transform.right * moveSpeed;
    52.     }
    53.  
    54.     void SetFlipY()
    55.     {
    56.         // I'm not going to base it off of rot but rather off of the
    57.         // sign of the x component of the transform.right vector.
    58.         bool flipy = transform.right.x < 0;
    59.         spriteRenderer.flipY = flipy;
    60.     }
    61. }
    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.
     
  3. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    hey dude thanks for replying

    got no control man, my plane just flies off the screen lol
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Did you set your turn to nonzero?
     
  5. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    yeah and speed too

    i like those types of games too!!!
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    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?
     
  7. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    when i move this line transform.Rotate(0, 0, rot * Time.deltaTime);

    out f here

    1. if (Input.GetMouseButton(0))
    2. {
    3. Vector3 mousePos = Input.mousePosition;
    4. mousePos.z = 1; // gotta cast a little bit into the scene!

    5. mousePos = Camera.main.ScreenToWorldPoint(mousePos);

    6. if (mousePos.x < 0)
    7. {
    8. rot = rotateAmount;
    9. }
    10. else
    11. {
    12. rot = -rotateAmount;
    13. }

    14. transform.Rotate(0, 0, rot * Time.deltaTime);
    15. }
    so after the if, it works!
     
  8. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    but not properly
     
  9. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    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!!!
     
    Kurt-Dekker likes this.
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Now go make some enemies, make some gunning, and blast 'em out of the sky! We're all counting on you Red Baron!
     
  11. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    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!
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    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:

    Code (csharp):
    1. SetFlipY();
    to instead be:

    Code (csharp):
    1. else
    2. {
    3.    SetFlipY();
    4. }
    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.
     
  13. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    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.
     
  14. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    Works like a dream, you are really gifted!!!