Search Unity

Space.world and space.self, whats the difference ?

Discussion in 'Getting Started' started by TheCelt, Mar 8, 2016.

  1. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    742
    So i am kinda new to 3D gaming development and am not understanding the difference between these two variables.

    World Applies transformation relative to the world coordinate system.
    Self Applies transformation relative to the local coordinate system.

    World coordinate system makes sense but what is a local coordinate system? What does it mean by "local"? What are the fundamental difference so i know which one to use for my camera movement script.

    For example i want to apply this to camera:
    transform.Translate(move, something); //move is a vector3

    Where by something is either Space.world or Space.self but am unsure which to use.
     
  2. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    Basically WolrdSpace is measured from the 0, 0,0 in unity. LocalSpace measure the current object's position AS IF it was 0, 0, 0.

    In regards to transform.Translate, if you leave 'something' out, it will work from WorldSpace. So if you want to move an object to the position 3, 5, 7 from the original 0, 0, 0 in the game, use WorldSpace (leave that part blank). If you want to move something to a relative position of a player/enemy/car/whatever, you can use local space or a mixture of transforms.
     
    gaolt930724 likes this.
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    @Tset_Tsyung is mostly correct, but there's more to it. 'Self' (local) space takes into account not just the object's position, but its rotation and scale, too.

    So if you say transform.Translate(Vector3.forward, Space.Self), this moves the object one unit in its own forward direction, whatever way that happens to be at the moment. If the object is pointing "northwest" then this moves it northwest. And this is the default, so it's what happens if you just say transform.Translate(Vector3.forward).

    But if you say transform.Translate(Vector3.forward, Space.World), then this adds Vector3(0, 0, 1) (which is the value of Vector3.forward) to the object's position, no matter which way it's facing. Its position in the world just shifts over one unit in Z.
     
    Tset_Tsyung likes this.
  4. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    742
    So for camera movement i should generally use world space?
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    That depends entirely on how you want it to move. I suggest you just try it and see; you'll know right away if you've chosen the wrong one.
     
    Tset_Tsyung and Kiwasi like this.
  6. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    742
    I'm not seeing much difference to be honest. I want raw scrolling, so no matter the camera distance the amount of movement is the same as how far the mouse moved. For some reason its a fractional amount compared to the mouse unless variable pan speed is a stupidly high number like 1000.

    I thought using mouse positions it would translate the camera equally as much.
     
    katarina442 likes this.
  7. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    Sorry, can I ask, does the camera rotate at all? Or is it a simple panning system (left, right, up, down)?

    Does the camera follow a particular object or character?
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Mouse positions are in (2D) screen coordinates. Camera (or any other game object) positions are in (3D) world coordinates. These are quite different.

    There are a number of ways to convert from one to the other, but without more information about your setup and what you're trying to do, I can't guess which would apply in your case.
     
  9. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    742
    My camera settings is set like this:

    Camera rotate:
    x: 30
    y: 0
    z: 0

    Orthographic projection.

    On load the camera Start() function sets the focus to look at a specific object but is not lock to it, it is free to move from user's mouse input after that.

    The camera does not follow anything it is a free camera looking over the scene, that i want the user to be able to move around freely.

    What would be the easiest way to get a more raw input movement between camera and mouse?

    I don't want the camera to move closer/further, so only a left/right/up/down situation is my goal here.

    Image provided:
     
  10. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    And how is your world going to be arranged? On the XZ plane, or on the XY plane?

    Either way, it's pretty clear you should be using Space.world here, so that the camera moves only in world XY (or XZ, depending on which you want), rather than moving at an angle according to its own rotation.
     
  11. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    742
    Not sure what you mean by arranged? Currently i move my objects around by x and y if thats what you mean ? Then again objects can also be orbiting at an angle.

    I come from a 2D image tile based development so incorporating a 3D camera is a bit confusing. I'll use world space though i still have to have pan speed variable to have a huge value =/
     
    Last edited: Mar 10, 2016
  12. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yes, I mean where are most of your objects that you're panning over. It sounds like they're in the XY plane, so that means your camera should pan in (world) X and Y, too. Its Z value should never change.

    Well, that's just because you're not converting between screen coordinates and world coordinates. But if it works for you, there's nothing wrong with saying "good enough for now" and moving on!
     
  13. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    742
    I don't like the good enough for now mentality it won't help me understand 3D from my 2D ways of thinking.

    If i have orbits that are angled around my sun isn't that also a Z plane for objects? If the camera is angled and my code is only moving the X:Y values why does the Z value change?

    Is it because screen coordinates for mouse has a changing Z value?
     
    Last edited: Mar 10, 2016
  14. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    No, screen coordinates always have Z=0. If your camera's Z values are changing, then I'd guess that you're moving relative to Space.self rather than Space.world.
     
  15. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    742
    Yes you're correct using Space.world fixed it. Though i still need to use panSpeed value of about 1000 and even thats not fast enough to keep up with the mouse.

    So i am not understanding why the camera won't move the same distance as the mouse on the X:Y directions, because it should be even without panSpeed value.
     
  16. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    It doesn't because the mouse position is in screen coordinates, i.e. pixels — those go from 0 to 1024 or however wide your screen/window is in X, and similar in Y. But the camera and object positions are in world coordinates. Exactly what those are depends on how you've set up your game, but typically, an object at something like X=10 will be off the screen.

    Your setup looks a little unusual; I see your camera size is 1000. That means, if I understand correctly, that there are 1000 world units between the center of the screen and the top (or bottom). So in your case, world units should actually be fairly close to screen coordinates — but not exact.

    There is no direct relationship between screen coordinates and world coordinates. And because screen coordinates are 2D and world coordinates are 3D, you need an extra piece of information to convert, that is, how far from the camera you want to imagine the 3D point to be. In other words, how far is it from the camera to the imaginary plane you want your world point to be in?

    Once you figure that out, then you can use Camera.ScreenToWorldPoint to convert your mouse position to a world position. Do this on each frame, and then by comparing the current world position to the previous world position, you can figure out how much to shift the camera.
     
  17. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    742
    Well the camera i plan to have a zoom function so surely the how far the camera is, depends on the zoom?

    The setup is like that because i want the camera zoomed far away at first, the objects are quite large to it's a solar system with reasonable scale sizes.