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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Need help with basic flight controlls

Discussion in 'Scripting' started by Kisslesshs, Oct 6, 2022.

  1. Kisslesshs

    Kisslesshs

    Joined:
    Sep 5, 2022
    Posts:
    2
    Hi, I am making a 2,5D simple flying game for a school project. However I am having problems getting the airplane to spine 180 d after the player presses "Space".
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Threading;
    4. using UnityEngine;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     private float speed = 10.0f;
    9.     public float rotationSpeed;
    10.     public float verticalInput;
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.        
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void LateUpdate()
    19.     {
    20.         // Players vertical input
    21.         verticalInput = Input.GetAxis("Vertical");
    22.  
    23.         // constant moving speed for the plane
    24.         transform.Translate(Vector3.forward * Time.deltaTime *speed);
    25.  
    26.         // move the plane up/down
    27.         transform.Rotate(Vector3.right * rotationSpeed * Time.deltaTime * verticalInput);
    28.        
    29.         //Spin the plane
    30.         if(Input.GetKey(KeyCode.Space))
    31.         {
    32.             Vector3 spin = new Vector3(0,0,180);
    33.             transform.Rotate(spin);
    34.             //transform.Rotate(0, 0, -180 * Time.deltaTime);
    35.         }
    36.     }
    37. }
    Vector3 spin = new Vector3(0,0,180);
    transform.Rotate(spin);
    makes the plane spin 180 but there is this wierd flickering in the plane model, and even sometimes stops the airplane in the air.

    tried also transform.Rotate(0, 0, -180 * Time.deltaTime); but this makes the plane rotate slowly and causes problems when it is a 2,5D game and the player starts flying in the wrong directions.

    Thanks in advance.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,955
    Line 32 rotates the plane a full 180 degrees every frame that you hold the button down.

    Do you mean you want Space to execute a loop?

    If so then there's a bunch of things to change:

    - read Input.GetKeyDown(), NOT the one you're using now

    - make sure you read it in Update()!!! That's the only place it is valid.

    - if Space is pressed, set a float
    loopDegreesRemaining
    variable to 360, indicating that's how much rolling you must do.

    - Every frame (LateUpdate() is fine), if that loop variable is greater than zero:
    ---> calculate how much to rotate this frame (use Time.deltaTime)
    ---> subtract that from the
    loopDegreesRemaining

    ---> if it was more, clamp it to what was remaining
    ---> rotate the plane by that amount
    ---> during this time, you must inhibit the vertical input (eg, don't run that code) to let the loop finish.
     
    Kisslesshs likes this.
  3. Kisslesshs

    Kisslesshs

    Joined:
    Sep 5, 2022
    Posts:
    2
    Thank you, I´ll have go during the weekend and try it out.