Cant get airplane turning up and down with buttons

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

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

public class PlaneController : MonoBehaviour
{
    Rigidbody2D rb;
    [Tooltip("World units per second.")]
    public float moveSpeed;
    [Tooltip("Degrees per second.")]
    public float rotateAmount;
    float rot;
    bool stalling = false;
    bool groundCrash = false;
    bool up = false;
    bool down = false;
   
    public Animator anim;

    public GameObject bullet1;
    private bool firingBullet1 = false;
    public float bulletSpeed = 500;
    public GameObject BulletSpawnObject;

    private Renderer[] renderers;
    private bool isWrappingX = false;

    SpriteRenderer spriteRenderer;

    void Start()
    {
        renderers = GetComponentsInChildren<Renderer>();
    }

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

    // Update is called once per frame
    void Update()
    {
        float angle = transform.eulerAngles.z;


        if (up)
        {
            rot = rotateAmount;
        }
        if (down)
        {
            rot = -rotateAmount;
           
        }

        else
        {
            SetFlipY();
        }

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

        if (stalling)
        {
            if (angle < 110 && angle > 70)
            {
                Debug.Log("Angle = " + angle);
                rb.gravityScale = 25;

                stalling = false;
            }
            else
            {
                rb.gravityScale = 15;

                stalling = false;
            }

            StartCoroutine(StallTime());
        }
       

        if (firingBullet1)
        {
            GameObject newBullet1 = Instantiate(bullet1, BulletSpawnObject.transform.position, transform.rotation);
            newBullet1.GetComponent<Rigidbody2D>().AddRelativeForce(Vector2.right * bulletSpeed);
            Destroy(newBullet1, 2.0F);
            FindObjectOfType<AudioManager>().Play("Gun1");
            firingBullet1 = false;
        }

        if (groundCrash)
        {
            FindObjectOfType<AudioManager>().Play("Explosion1");
            groundCrash = false;
            anim.SetBool("Crashed", true);
        }
    }

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

    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;
    }

   void ScreenWrap()
    {
        bool isVisible = CheckRenderers();

        if (isVisible)
        {
            isWrappingX = false;
            return;
        }

        if (isWrappingX)
        {
            return;
        }

        Vector3 newPosition = transform.position;

        if (newPosition.x > 1 || newPosition.x < 0)
        {
            newPosition.x = -newPosition.x;
            isWrappingX = true;
        }

        transform.position = newPosition;
    }

    bool CheckRenderers()
    {
        foreach (Renderer renderer in renderers)
        {
            if (renderer.isVisible)
            {
                return true;
            }
        }

        return false;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "HeightBorder")
        {
            stalling = true;
        }

        if (collision.gameObject.tag == "GroundBorder")
        {
            groundCrash = true;
        }


    }

    IEnumerator StallTime()
    {
        //float randomTime = Random.Range(3f, 10f);
        yield return new WaitForSeconds(0.5f);
        rb.gravityScale = 0;
    }

    public void FireBullet()
    {
        firingBullet1 = true;
    }

    public void LeftDown()
    {
        up = true;
    }

    public void LeftUp()
    {
        up = false;
    }

    public void RightUp()
    {
        down = true;
    }

    public void RightDown()
    {
        down = false;
    }

}

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.

cool thanks kurt

though a lot better, it is still turning on its own:

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

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

    public float currentRotationSpeed;
    float desiredRotationSpeed;
    bool turning = false;
   
   
    public Animator anim;

    public GameObject bullet1;
    private bool firingBullet1 = false;
    public float bulletSpeed = 500;
    public GameObject BulletSpawnObject;

    private Renderer[] renderers;
    private bool isWrappingX = false;

    SpriteRenderer spriteRenderer;

    void Start()
    {
        renderers = GetComponentsInChildren<Renderer>();
    }

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

    // Update is called once per frame
    void Update()
    {
        float angle = transform.eulerAngles.z;


        if (turning)
        {
            currentRotationSpeed = Mathf.MoveTowards(currentRotationSpeed, desiredRotationSpeed, moveSpeed);
        }

        else
        {
            SetFlipY();
        }

        transform.Rotate(0, 0, currentRotationSpeed * Time.deltaTime);

        if (stalling)
        {
            if (angle < 110 && angle > 70)
            {
                Debug.Log("Angle = " + angle);
                rb.gravityScale = 25;

                stalling = false;
            }
            else
            {
                rb.gravityScale = 15;

                stalling = false;
            }

            StartCoroutine(StallTime());
        }
       

        if (firingBullet1)
        {
            GameObject newBullet1 = Instantiate(bullet1, BulletSpawnObject.transform.position, transform.rotation);
            newBullet1.GetComponent<Rigidbody2D>().AddRelativeForce(Vector2.right * bulletSpeed);
            Destroy(newBullet1, 2.0F);
            FindObjectOfType<AudioManager>().Play("Gun1");
            firingBullet1 = false;
        }

        if (groundCrash)
        {
            FindObjectOfType<AudioManager>().Play("Explosion1");
            groundCrash = false;
            anim.SetBool("Crashed", true);
        }
    }

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

    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;
    }

   void ScreenWrap()
    {
        bool isVisible = CheckRenderers();

        if (isVisible)
        {
            isWrappingX = false;
            return;
        }

        if (isWrappingX)
        {
            return;
        }

        Vector3 newPosition = transform.position;

        if (newPosition.x > 1 || newPosition.x < 0)
        {
            newPosition.x = -newPosition.x;
            isWrappingX = true;
        }

        transform.position = newPosition;
    }

    bool CheckRenderers()
    {
        foreach (Renderer renderer in renderers)
        {
            if (renderer.isVisible)
            {
                return true;
            }
        }

        return false;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "HeightBorder")
        {
            stalling = true;
        }

        if (collision.gameObject.tag == "GroundBorder")
        {
            groundCrash = true;
        }


    }

    IEnumerator StallTime()
    {
        //float randomTime = Random.Range(3f, 10f);
        yield return new WaitForSeconds(0.5f);
        rb.gravityScale = 0;
    }

    public void FireBullet()
    {
        firingBullet1 = true;
    }

    public void LeftDown()
    {
        desiredRotationSpeed = 100;
        turning = true;
    }

    public void LeftUp()
    {
        turning = false;
    }

    public void RightDown()
    {
        desiredRotationSpeed = -100;
        turning = true;
    }

    public void RightUp()
    {
        turning = false;
    }

   

}

Line 53 must unconditionally run. You want current to ALWAYS chase towards desired.

k

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!

1 Like

Look at you, makin’ games!

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.

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.

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 :slight_smile:

as you can see the stall sucks!

https://vimeo.com/469332648

1 Like

Feels good, doesn’t it? :slight_smile:

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.

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!

1 Like