0
\$\begingroup\$

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 plane does not move.

using UnityEngine;

public class PlaneController : MonoBehaviour
{
public float speed = 5f; // Speed of movement
public float rotationSpeed = 100f; // Speed of rotation

private void Start()
{
    // Create a new GameObject for the plane
    GameObject plane = new GameObject("Plane");

    // Add BoxCollider component to the plane
    BoxCollider collider = plane.AddComponent<BoxCollider>();
    collider.size = new Vector3(1f, 0.01f, 1f);
    collider.center = Vector3.zero;

    // Set the plane's position and rotation
    plane.transform.position = Vector3.zero;
    plane.transform.rotation = Quaternion.identity;

    // Set the plane as a child of the current GameObject
    plane.transform.parent = transform;

    // Move and rotate the plane based on input
    void Update()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");
        float rotate = Input.GetAxis("Rotate");

        // Calculate movement and rotation
        Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical) * speed * Time.deltaTime;
        Quaternion rotation = Quaternion.Euler(0f, rotate * rotationSpeed * Time.deltaTime, 0f);

        // Apply movement and rotation to the plane
        plane.transform.Translate(movement, Space.World);
        plane.transform.Rotate(rotation.eulerAngles, Space.World);
    }
  }
}

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.

using UnityEngine;

public class PlaneController : MonoBehaviour
{
    public float speed = 5f; // Speed of movement
    public float rotationSpeed = 100f; // Speed of rotation

    private void Update()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");
        float rotate = Input.GetAxis("Rotate");

        // Calculate movement and rotation
        Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical) * speed * Time.deltaTime;
        Quaternion rotation = Quaternion.Euler(0f, rotate * rotationSpeed * Time.deltaTime, 0f);

        // Apply movement and rotation to the plane
        transform.Translate(movement, Space.World);
        transform.Rotate(rotation.eulerAngles, Space.World);
    }
}
\$\endgroup\$
7
  • 2
    \$\begingroup\$ Is Update() a local function of Start()? \$\endgroup\$
    – Mangata
    Jun 6, 2023 at 2:55
  • \$\begingroup\$ Is your problem the rotation, or is your problem that Update is not getting called? \$\endgroup\$
    – DMGregory
    Jun 6, 2023 at 13:02
  • \$\begingroup\$ It looks like Update() is inside Start(). You need a '}' to end the Start() code block first \$\endgroup\$ Jun 6, 2023 at 14:20
  • \$\begingroup\$ I think those comments could be an answer, Mangata and I_Keep_Trying. \$\endgroup\$
    – DMGregory
    Jun 6, 2023 at 17:28
  • 1
    \$\begingroup\$ From where are you reading the Input.GetAxis("Rotate")? \$\endgroup\$
    – Zibelas
    Jun 11, 2023 at 7:35

2 Answers 2

0
\$\begingroup\$

I ran your code, but instead of using the Input system I just used a Vector3(.25f, 0.5f, 1f) for the Translate and it rockets away. For rotate I just pass a constant float of 2f, and the plane indeed rotates along its y axis. Everything "works". The script is of course attached to a plane object.

Its movement is straight away from its starting position, without any regard to its rotation. That is because you are using Space.World in your translate function- if you want the plane to fly in the direction it is facing, you would need to use Space.Self, which is the default value and does not need to be set. The same is also true for Rotate, but it depends what you are trying to accomplish with that.

My first game had objects racing around a central point. At that time I just positioned them away from the center of a parent object which was rotating along the Y axis. If I wanted them to move closer to the inside or outside of the track, I changed the localPosition of the child "racer" object. It had very simplified controls but it's an example of the many ways to rotate something around an origin in world space.

If that doesn't allow for complex enough movement, manually calculating a position around a central point based on rotations and a radius involves a good familiarity with trig functions and their relationship to the unit circle. If you understand that Sin gives you Y values and Cos gives you X values of positions along a circle based on an input of your rotation in radians, then you can work out your Z and X positions in unity that way. At that point however, you might find it far easier not to swing your plane around an origin point but rather to just drive it relative to itself, like an airplane, only limiting its range of direction and steering.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for the info (I will take some time to read it carefully). Do you have any suggestions to get this to bind to the input manager? 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. \$\endgroup\$ Jun 11, 2023 at 22:04
  • \$\begingroup\$ Yes, it's important to know that these axes are assigned by the dev in unity. Unity keeps a list of all the axes, and the controls that affect them, in Edit > Project Settings > Input Manager > Axes. Have a look at some of those entries, assign some keys, and try them out with your code. The name that they are assigned in Unity is the exact name they will use in quotes in the code. \$\endgroup\$
    – Wiley Rush
    Jun 13, 2023 at 14:02
0
\$\begingroup\$

The reason why no movement at all is working is that there is no Rotation Input. Unity is even telling you this with an error message (which should be included in the question)

enter image description here

How should a rotation input look like? You can always rotate towards a direction, a pure rotation input would be something like a twisting knob and that would fail to get the full rotation to be captured in a single Update.

You can see the full supported input commands under Edit - Settings - Input (as per error message)

enter image description here

If you have made a custom Rotation Input there, you would need to share that one.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .