Space.World / Space.Self inverted? [SOLVED]

Hi,

When I use transform.Rotate with Space.Self (or no Space. defined) my object is being rotated along GLOBAL axes, and when I use Space.World - it rotates along LOCAL axes.

As I understand, it should be the other way around.
Is it possible that I activated some sort of inversion by accident? How can I fix this?

VIDEO AND CODE BELOW

Works perfectly fine for me, just as expected.
Could you post some code that produces these results for you?

Also, check the local/global option in the top left corner. It allows to switch between the visualization of local and world axis (but it doesn’t change any behaviour) - perhaps this leads to confusion.

And the code: (the broken version, with Space.Self, that should work correctly, if I understand it right)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Latacz_2_Controller : MonoBehaviour {
    // airplane
    private Rigidbody myRigidbody;
    //test
    public float testSpeed;
    void Start () 
    {
        myRigidbody = GetComponent<Rigidbody> ();
    }
    void Update () 
    {
        PlayerControl();
    }
    // player control system
    void PlayerControl()
    {
       
        // AXES HELP
        Debug.DrawRay(transform.position, transform.forward * 100f, Color.blue);
        Debug.DrawRay(transform.position, transform.up * 100f, Color.green);
        Debug.DrawRay(transform.position, transform.right * 100f, Color.red);
        // STEERING
        // PITCH
        if (Input.GetKey(KeyCode.DownArrow))
        {
            transform.Rotate(-transform.right, testSpeed * Time.deltaTime, Space.Self);
        }
        if (Input.GetKey(KeyCode.UpArrow))
        {
            transform.Rotate(transform.right, testSpeed * Time.deltaTime, Space.Self);
        }
        // ROLL
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.Rotate(transform.forward, testSpeed * Time.deltaTime, Space.Self);
        }
        if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.Rotate(-transform.forward, testSpeed * Time.deltaTime, Space.Self);
        }
        // YAW
        if (Input.GetKey(KeyCode.E))
        {
            transform.Rotate(transform.up, testSpeed * Time.deltaTime, Space.Self);
        }
        if (Input.GetKey(KeyCode.Q))
        {
            transform.Rotate(-transform.up, testSpeed * Time.deltaTime, Space.Self);
        }
       
    }
}

It’s a bug in your code.
A transform’s properties up, down and such return the corresponding local axis in world space representation.

Example:
Suppose an object is rotated 90 degrees around the y-axis (clock-wise), i.e. it looks into the x-direction (object’s local forward direction is aligned with x-axis).

A call to transform.forward does now return (1, 0, 0)

Let’s start to rotate it:

Rotate the object around its own forward in Space.World => it’s actually going to rotate around the world space’s x-axis, transform.forward (local z-axis) stays aligned with the world space’s x-axis, hence it’ll continue to return (1, 0, 0) and you’ll see no weird rotations… but it looks as if it rotated locally.

Rotate the object around its own forward in Space.Local => it’s going to look very strange.
The reason is, that it takes the transform’s forward (local z-axis), which is (1, 0, 0) in world space again. But this value is going to be used in local space now, that is, the object rotates around its local x-axis, which is the z-axis in world space - and this does only apply to the first frame.

Due to that rotation, the next time you call transform.forward, it’ll now return the object’s local forward again, but it’s no longer (1, 0, 0). Again, this will locally be the z-axis, but in world space it’s going to be something like (x, 0, z).

*Edit
Of course I missed an important part: a possible solution :smile:
Try to pass Vector3.up, down and so on directly, do not use the transform.forward, because they’re not going to stay the same in world space (remember, they always return the direction in world space).

1 Like

Yes, Vector3.up etc. was exactly what I needed. Thanks!

What confused me, was that transform.forward etc. helper lines that I draw, were moving along with the fighter, so I thought I should rotate it along them :smile: