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

Question How to rotate a plane about its origin?

Discussion in 'Scripting' started by nill_milk, Jun 6, 2023.

  1. nill_milk

    nill_milk

    Joined:
    Apr 28, 2020
    Posts:
    4
    I am trying to figure out how to rotate a plane about its origin in 3D space. Here is the last iteration of the code I am working with; the aircraft does not move.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlaneController : MonoBehaviour
    4. {
    5.     public float speed = 5f; // Speed of movement
    6.     public float rotationSpeed = 100f; // Speed of rotation
    7.  
    8.     private void Start()
    9.     {
    10.         // Create a new GameObject for the plane
    11.         GameObject plane = new GameObject("Plane");
    12.  
    13.         // Add BoxCollider component to the plane
    14.         BoxCollider collider = plane.AddComponent<BoxCollider>();
    15.         collider.size = new Vector3(1f, 0.01f, 1f);
    16.         collider.center = Vector3.zero;
    17.  
    18.         // Set the plane's position and rotation
    19.         plane.transform.position = Vector3.zero;
    20.         plane.transform.rotation = Quaternion.identity;
    21.  
    22.         // Set the plane as a child of the current GameObject
    23.         plane.transform.parent = transform;
    24.  
    25.         // Move and rotate the plane based on input
    26.         void Update()
    27.         {
    28.             float moveHorizontal = Input.GetAxis("Horizontal");
    29.             float moveVertical = Input.GetAxis("Vertical");
    30.             float rotate = Input.GetAxis("Rotate");
    31.  
    32.             // Calculate movement and rotation
    33.             Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical) * speed * Time.deltaTime;
    34.             Quaternion rotation = Quaternion.Euler(0f, rotate * rotationSpeed * Time.deltaTime, 0f);
    35.  
    36.             // Apply movement and rotation to the plane
    37.             plane.transform.Translate(movement, Space.World);
    38.             plane.transform.Rotate(rotation.eulerAngles, Space.World);
    39.         }
    40.     }
    41. }
     
  2. Reaktion

    Reaktion

    Joined:
    Nov 8, 2019
    Posts:
    44
    Hi,

    As I can see here, your Update function is inside your Start function.
    You should put your Update function inside the PlaneController class but outside the Start function.
    (That's how Unity will know that Update is implemented inside your class, and will call it every frame.)

    Once you do that, you should realize that you won't be able to use the plane variable : if you need to use it in multiple function inside the same class, put it as a class variable.
     
    Bunny83 and orionsyndrome like this.
  3. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Indeed, your code is so neat and orderly (which is great), I'm not sure how you don't see that your Update is inside Start.
     
    Bunny83 likes this.
  4. nill_milk

    nill_milk

    Joined:
    Apr 28, 2020
    Posts:
    4
    UPDATE:

    I updated my code to this, and the plane is still not moving. Any help in what I am doing incorrectly would be greatly appreciated.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlaneController : MonoBehaviour
    4. {
    5.    public float speed = 5f; // Speed of movement
    6.    public float rotationSpeed = 100f; // Speed of rotation
    7.  
    8.    private void Update()
    9.    {
    10.        float moveHorizontal = Input.GetAxis("Horizontal");
    11.        float moveVertical = Input.GetAxis("Vertical");
    12.        float rotate = Input.GetAxis("Rotate");
    13.  
    14.        // Calculate movement and rotation
    15.        Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical) * speed * Time.deltaTime;
    16.        Quaternion rotation = Quaternion.Euler(0f, rotate * rotationSpeed * Time.deltaTime, 0f);
    17.  
    18.        // Apply movement and rotation to the plane
    19.        transform.Translate(movement, Space.World);
    20.        transform.Rotate(rotation.eulerAngles, Space.World);
    21.    }
    22. }
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,560
    This is your clue that it is now...

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all <--- this is my bet
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is <-- or maybe this?
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
  6. nill_milk

    nill_milk

    Joined:
    Apr 28, 2020
    Posts:
    4
    @Kurt-Dekker I posted this topic on https://gamedev.stackexchange.com/q...rotate-a-plane-about-its-origin/205941#205941, and Someone was able to get the plane to go, buy bypassing the Input Manager, So I think that is where I should start debugging, do you have any ideas on how to get the Input Manager to see my input? My understanding is The Input.GetAxis function retrieves input values from the Input Manager based on the specified input axis name. For example, Input.GetAxis("Horizontal") reads horizontal movement, Input.GetAxis("Vertical") is used for vertical direction and Input.GetAxis("Rotate") is used for rotation
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,560
    You should be using the methods listed above to TEST your understanding, not posting in English prose what your understanding is.

    Remember, your understanding is irrelevant compared to the computer's understanding of it.

    Take it full loop. Use Debug.Log() to understand what you're doing, be confident in it.