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

Cant get airplane turning up and down with buttons

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

  1. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    i have a 2d plane thats side view, trying to get the plane to rotate clockwise when the right button is pressed and anti clockwise when the left button is pressed

    i have 2 buttons each have an EventTrigger
    1 for up and 1 for down
    but it doesnt turn incrementally, it just starts turning max as soon as you press the button

    i cant work out how to move incrementally

    please help

    here is my script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class PlaneController : MonoBehaviour
    7. {
    8.     Rigidbody2D rb;
    9.     [Tooltip("World units per second.")]
    10.     public float moveSpeed;
    11.     [Tooltip("Degrees per second.")]
    12.     public float rotateAmount;
    13.     float rot;
    14.     bool stalling = false;
    15.     bool groundCrash = false;
    16.     bool up = false;
    17.     bool down = false;
    18.    
    19.     public Animator anim;
    20.  
    21.     public GameObject bullet1;
    22.     private bool firingBullet1 = false;
    23.     public float bulletSpeed = 500;
    24.     public GameObject BulletSpawnObject;
    25.  
    26.     private Renderer[] renderers;
    27.     private bool isWrappingX = false;
    28.  
    29.     SpriteRenderer spriteRenderer;
    30.  
    31.     void Start()
    32.     {
    33.         renderers = GetComponentsInChildren<Renderer>();
    34.     }
    35.  
    36.     private void Awake()
    37.     {
    38.         rb = GetComponent<Rigidbody2D>();
    39.         spriteRenderer = GetComponent<SpriteRenderer>();
    40.     }
    41.  
    42.     // Update is called once per frame
    43.     void Update()
    44.     {
    45.         float angle = transform.eulerAngles.z;
    46.  
    47.  
    48.         if (up)
    49.         {
    50.             rot = rotateAmount;
    51.         }
    52.         if (down)
    53.         {
    54.             rot = -rotateAmount;
    55.            
    56.         }
    57.  
    58.         else
    59.         {
    60.             SetFlipY();
    61.         }
    62.  
    63.         transform.Rotate(0, 0, rot * Time.deltaTime);
    64.  
    65.         if (stalling)
    66.         {
    67.             if (angle < 110 && angle > 70)
    68.             {
    69.                 Debug.Log("Angle = " + angle);
    70.                 rb.gravityScale = 25;
    71.  
    72.                 stalling = false;
    73.             }
    74.             else
    75.             {
    76.                 rb.gravityScale = 15;
    77.  
    78.                 stalling = false;
    79.             }
    80.  
    81.             StartCoroutine(StallTime());
    82.         }
    83.        
    84.  
    85.         if (firingBullet1)
    86.         {
    87.             GameObject newBullet1 = Instantiate(bullet1, BulletSpawnObject.transform.position, transform.rotation);
    88.             newBullet1.GetComponent<Rigidbody2D>().AddRelativeForce(Vector2.right * bulletSpeed);
    89.             Destroy(newBullet1, 2.0F);
    90.             FindObjectOfType<AudioManager>().Play("Gun1");
    91.             firingBullet1 = false;
    92.         }
    93.  
    94.         if (groundCrash)
    95.         {
    96.             FindObjectOfType<AudioManager>().Play("Explosion1");
    97.             groundCrash = false;
    98.             anim.SetBool("Crashed", true);
    99.         }
    100.     }
    101.  
    102.     private void FixedUpdate()
    103.     {
    104.         rb.velocity = transform.right * moveSpeed;
    105.         ScreenWrap();
    106.     }
    107.  
    108.     void SetFlipY()
    109.     {
    110.         // I'm not going to base it off of rot but rather off of the
    111.         // sign of the x component of the transform.right vector.
    112.         bool flipy = transform.right.x < 0;
    113.         spriteRenderer.flipY = flipy;
    114.     }
    115.  
    116.    void ScreenWrap()
    117.     {
    118.         bool isVisible = CheckRenderers();
    119.  
    120.         if (isVisible)
    121.         {
    122.             isWrappingX = false;
    123.             return;
    124.         }
    125.  
    126.         if (isWrappingX)
    127.         {
    128.             return;
    129.         }
    130.  
    131.         Vector3 newPosition = transform.position;
    132.  
    133.         if (newPosition.x > 1 || newPosition.x < 0)
    134.         {
    135.             newPosition.x = -newPosition.x;
    136.             isWrappingX = true;
    137.         }
    138.  
    139.         transform.position = newPosition;
    140.     }
    141.  
    142.     bool CheckRenderers()
    143.     {
    144.         foreach (Renderer renderer in renderers)
    145.         {
    146.             if (renderer.isVisible)
    147.             {
    148.                 return true;
    149.             }
    150.         }
    151.  
    152.         return false;
    153.     }
    154.  
    155.     private void OnCollisionEnter2D(Collision2D collision)
    156.     {
    157.         if (collision.gameObject.tag == "HeightBorder")
    158.         {
    159.             stalling = true;
    160.         }
    161.  
    162.         if (collision.gameObject.tag == "GroundBorder")
    163.         {
    164.             groundCrash = true;
    165.         }
    166.  
    167.  
    168.     }
    169.  
    170.     IEnumerator StallTime()
    171.     {
    172.         //float randomTime = Random.Range(3f, 10f);
    173.         yield return new WaitForSeconds(0.5f);
    174.         rb.gravityScale = 0;
    175.     }
    176.  
    177.     public void FireBullet()
    178.     {
    179.         firingBullet1 = true;
    180.     }
    181.  
    182.     public void LeftDown()
    183.     {
    184.         up = true;
    185.     }
    186.  
    187.     public void LeftUp()
    188.     {
    189.         up = false;
    190.     }
    191.  
    192.     public void RightUp()
    193.     {
    194.         down = true;
    195.     }
    196.  
    197.     public void RightDown()
    198.     {
    199.         down = false;
    200.     }
    201.  
    202. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    You want two variables:

    -
    currentRotationSpeed

    -
    desiredRotationSpeed


    When you process user input, you set
    desiredRotationSpeed
    to what it should be (the way you do now with
    rot
    when processing up / down)

    Every
    Update()
    you use
    Mathf.MoveTowards()
    to move
    currentRotationSpeed
    gradually to
    desiredRotationSpeed
    .

    To turn your plane you use
    currentRotationSpeed
    .
     
  3. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    cool thanks kurt
     
  4. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    though a lot better, it is still turning on its own:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class PlaneController : MonoBehaviour
    7. {
    8.     Rigidbody2D rb;
    9.     [Tooltip("World units per second.")]
    10.     public float moveSpeed;
    11.     [Tooltip("Degrees per second.")]
    12.     public float rotateAmount;
    13.    
    14.     bool stalling = false;
    15.     bool groundCrash = false;
    16.  
    17.     public float currentRotationSpeed;
    18.     float desiredRotationSpeed;
    19.     bool turning = false;
    20.    
    21.    
    22.     public Animator anim;
    23.  
    24.     public GameObject bullet1;
    25.     private bool firingBullet1 = false;
    26.     public float bulletSpeed = 500;
    27.     public GameObject BulletSpawnObject;
    28.  
    29.     private Renderer[] renderers;
    30.     private bool isWrappingX = false;
    31.  
    32.     SpriteRenderer spriteRenderer;
    33.  
    34.     void Start()
    35.     {
    36.         renderers = GetComponentsInChildren<Renderer>();
    37.     }
    38.  
    39.     private void Awake()
    40.     {
    41.         rb = GetComponent<Rigidbody2D>();
    42.         spriteRenderer = GetComponent<SpriteRenderer>();
    43.     }
    44.  
    45.     // Update is called once per frame
    46.     void Update()
    47.     {
    48.         float angle = transform.eulerAngles.z;
    49.  
    50.  
    51.         if (turning)
    52.         {
    53.             currentRotationSpeed = Mathf.MoveTowards(currentRotationSpeed, desiredRotationSpeed, moveSpeed);
    54.         }
    55.  
    56.         else
    57.         {
    58.             SetFlipY();
    59.         }
    60.  
    61.         transform.Rotate(0, 0, currentRotationSpeed * Time.deltaTime);
    62.  
    63.         if (stalling)
    64.         {
    65.             if (angle < 110 && angle > 70)
    66.             {
    67.                 Debug.Log("Angle = " + angle);
    68.                 rb.gravityScale = 25;
    69.  
    70.                 stalling = false;
    71.             }
    72.             else
    73.             {
    74.                 rb.gravityScale = 15;
    75.  
    76.                 stalling = false;
    77.             }
    78.  
    79.             StartCoroutine(StallTime());
    80.         }
    81.        
    82.  
    83.         if (firingBullet1)
    84.         {
    85.             GameObject newBullet1 = Instantiate(bullet1, BulletSpawnObject.transform.position, transform.rotation);
    86.             newBullet1.GetComponent<Rigidbody2D>().AddRelativeForce(Vector2.right * bulletSpeed);
    87.             Destroy(newBullet1, 2.0F);
    88.             FindObjectOfType<AudioManager>().Play("Gun1");
    89.             firingBullet1 = false;
    90.         }
    91.  
    92.         if (groundCrash)
    93.         {
    94.             FindObjectOfType<AudioManager>().Play("Explosion1");
    95.             groundCrash = false;
    96.             anim.SetBool("Crashed", true);
    97.         }
    98.     }
    99.  
    100.     private void FixedUpdate()
    101.     {
    102.         rb.velocity = transform.right * moveSpeed;
    103.         ScreenWrap();
    104.     }
    105.  
    106.     void SetFlipY()
    107.     {
    108.         // I'm not going to base it off of rot but rather off of the
    109.         // sign of the x component of the transform.right vector.
    110.         bool flipy = transform.right.x < 0;
    111.         spriteRenderer.flipY = flipy;
    112.     }
    113.  
    114.    void ScreenWrap()
    115.     {
    116.         bool isVisible = CheckRenderers();
    117.  
    118.         if (isVisible)
    119.         {
    120.             isWrappingX = false;
    121.             return;
    122.         }
    123.  
    124.         if (isWrappingX)
    125.         {
    126.             return;
    127.         }
    128.  
    129.         Vector3 newPosition = transform.position;
    130.  
    131.         if (newPosition.x > 1 || newPosition.x < 0)
    132.         {
    133.             newPosition.x = -newPosition.x;
    134.             isWrappingX = true;
    135.         }
    136.  
    137.         transform.position = newPosition;
    138.     }
    139.  
    140.     bool CheckRenderers()
    141.     {
    142.         foreach (Renderer renderer in renderers)
    143.         {
    144.             if (renderer.isVisible)
    145.             {
    146.                 return true;
    147.             }
    148.         }
    149.  
    150.         return false;
    151.     }
    152.  
    153.     private void OnCollisionEnter2D(Collision2D collision)
    154.     {
    155.         if (collision.gameObject.tag == "HeightBorder")
    156.         {
    157.             stalling = true;
    158.         }
    159.  
    160.         if (collision.gameObject.tag == "GroundBorder")
    161.         {
    162.             groundCrash = true;
    163.         }
    164.  
    165.  
    166.     }
    167.  
    168.     IEnumerator StallTime()
    169.     {
    170.         //float randomTime = Random.Range(3f, 10f);
    171.         yield return new WaitForSeconds(0.5f);
    172.         rb.gravityScale = 0;
    173.     }
    174.  
    175.     public void FireBullet()
    176.     {
    177.         firingBullet1 = true;
    178.     }
    179.  
    180.     public void LeftDown()
    181.     {
    182.         desiredRotationSpeed = 100;
    183.         turning = true;
    184.     }
    185.  
    186.     public void LeftUp()
    187.     {
    188.         turning = false;
    189.     }
    190.  
    191.     public void RightDown()
    192.     {
    193.         desiredRotationSpeed = -100;
    194.         turning = true;
    195.     }
    196.  
    197.     public void RightUp()
    198.     {
    199.         turning = false;
    200.     }
    201.  
    202.    
    203.  
    204. }
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Line 53 must unconditionally run. You want current to ALWAYS chase towards desired.
     
  6. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
  7. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    OK its perfect thanks again, learnt so much with that

    just had to add
    desiredRotationSpeed = 0;
    in the button up, and its working like a dream

    adding a tank next! let the games begin!
     
    Kurt-Dekker likes this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Look at you, makin' games!
     
  9. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    Got bombs dropping and everything will post another demo shortly lol... I'm wondering how I can get bullets to puff up dust when they hit the ground.
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    ParticleSystem! Make one where the bullet hits. Make sure to kill it off after a few seconds, perhaps with a Destroy() command that includes the optional "how many seconds to wait until destruction" argument.
     
  11. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    Ha Ha actually there is tons of stuff i need to do before i even look at enemies

    still got to sort the stalling out, its a bit of a mess and the weapon spawning is not perfect

    its easy to get ahead of yourself with the excitement of getting things right lol!!!

    heres the latest demo :)

    as you can see the stall sucks!

     
    Kurt-Dekker likes this.
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Feels good, doesn't it? :)

    Hey, not sure if I mentioned it before, but make sure you either use source control (something like git) or just regular make zip backups.

    I highly recommend source control. It's easy to set git up with Unity and they play nice; lots of tutorials. Worst thing ever is to break something and spend hours trying to fix it, when with source control you press a button and revert the problem to a known good commit.
     
  13. epochplus5

    epochplus5

    Joined:
    Apr 19, 2020
    Posts:
    677
    im just using zip files now, will setup git at some stage. I copy the folder after a significant breakthrough and up it to my g drive. thanks man!
     
    Kurt-Dekker likes this.