Search Unity

Creating a Turn Table or Lazy Susan

Discussion in 'Scripting' started by Shadowing, Feb 10, 2016.

  1. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Anyone have any ideas on how to go about creating something like this.
    The issue I'm having is making a sprite rotate around while it keeps facing the screen.
    the function RotateAround doesn't keep facing the screen.



     
  2. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    Code (csharp):
    1.  
    2. public Transform[] planes;
    3. public Vector3[] offsets = new[] { Vector3.forward, Vector3.right, Vector3.back, Vector3.left };
    4. public float turnSpeed = 10.0f;  //how fast the turntable spins (in degrees)
    5.  
    6. void Update()
    7. {
    8.   for(int i = 0; i < planes.Length; i++)
    9.   {
    10.     Debug.Assert(offsets.Length == planes.Length, "We need an offset for each plane!");
    11.  
    12.     //rotate each plane around the y-axis -- this will set the plane position
    13.     var tableRotation = Quaternion.Euler(0f, Time.time * turnSpeed, 0f);
    14.     planes[i].position = tableRotation * offsets[i];
    15.    
    16.     //after we set the position, then we force each plane to look at the camera
    17.     planes[i].LookAt(Camera.main.transform);
    18.   }
    19. }
    20.  
     
    Kiwasi likes this.
  3. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Thanks for the reply man. Been sitting here for a hour messing with your example.

    I'm not using a camera so I just created a random object and placed it where I want things to face. but that was still wacky.
    I commented out LookAt() and the objects still look at the screen so that's not even needed.

    The rotate is really weird idk what I'm doing wrong.
    blue number 1 never comes up front. Plus it seems the z axis isn't changing.
    should I be using a Canvas or what.

    Right now im using 4 images inside a canvas



    Code (csharp):
    1.  
    2. for(int i = 0; i < planes.Length; i++){
    3.  
    4.  
    5.    Debug.Assert(offsets.Length == planes.Length, "We need an offset for each plane!");
    6.  
    7.    //rotate each plane around the y-axis -- this will set the plane position
    8.  
    9.    var tableRotation = Quaternion.Euler(0f, Time.time * turnSpeed, 0f);
    10.  
    11.    planes[i].position = tableRotation * offsets[i];
    12.  
    13.    //after we set the position, then we force each plane to look at the camera
    14.  
    15.    //planes[i].LookAt(TurnTableLookAt);
    16.  
    17. }
    18.  
    19.  
    20.  
     
  4. bajeo88

    bajeo88

    Joined:
    Jul 4, 2012
    Posts:
    64
    I believe when a canvas is rendered the child objects are rendered in hierarchy order top -> bottom. So your Z position is essentially being ignored. I would remove the canvas and just make them sprites, should work then!
     
  5. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Ya ditching the canvas fixed it. It doesn't change the z axis enough though. To give it more depth
     
  6. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    I'm porting my browser base game to Unity and I was using a jquery plug in that was real nice.
    This is what I'm trying to do

     
  7. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    So right now its exactly like that except the z axis isn't as in depth. Which makes sense JavaScript is doing fake z index and this is using actual world space.
     
  8. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Opps I had my player set to 2d view lmao my bad. It does give depth now. Ok so now I'm just left with one last issue. giving it a tilt. Not totally sure how to do that lol. My thinking I could just adjust a parent game objects rotation but that doesn't work at all lol.
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I prefer this line

    Code (CSharp):
    1. transform.rotation = Camera.main.rotation;
     
  10. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    I'm running into a lot of issues doing this as a world objects.. Think im going to have to have a separate camera and display the camera on the canvas. Then put hidden buttons for left and right rotate idk.

    This way I can have a background picture on the canvas behind it.
     
    Last edited: Feb 10, 2016
  11. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    If you want a tilt, why not just move your camera a bit up and tilt it down?
     
  12. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Well I got the turn table all figured out.
    I have another issue though

    So the camera is displaying on my game window
    But its not displaying on my Scene Window.
    I don't know what the heck. Driving me crazy lol.

    my canvas is set to Screen Space Overlay
    Anyone have any ideas what I can check.

    Ive displayed cameras on canvas before with no issue. Not sure what I'm missing.
    I'm sure its something simple.
    Course its displaying right in game just not in the scene editor view.






     
  13. NotTesla

    NotTesla

    Joined:
    Feb 1, 2015
    Posts:
    24
    *gasp* it's a stargate game!!!

    I also noticed you called the "Unas," "Onac," which is what the Unas call the goa'uld, not what the Unas are actually called.
     
  14. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    The game takes place in the past. Onac are a group of gouald that still use the unas as hosts. The game tells a story of a war between the Goa'uld and thge Furlings

    Evern though I love to talk about my game :)
    Like to keep this thread on the topic at hand though.
    The game is live here www.systemlords.com
    I'm just porting it now to unity

    Really can't figure this scene window issue
     
    Last edited: Feb 11, 2016
  15. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Hey Flaminghairball
    I'm having troubles understanding how this rotation works.
    Cause I don't understand it. I am having issues fixing a bug with it.
    Right now when it starts rotating it does a huge jump to the left or right.
    So it doesn't start rotating exactly where the planes are at.




    Code (csharp):
    1.  
    2.    for(int i = 0; i < planes.Length; i++){
    3.  
    4.    If(TurnTableDirection == "Left") {
    5.  
    6.       Quaternion tableRotation = Quaternion.Euler(0f, Time.time * turnSpeed, 0f);
    7.       planes[i].localPosition = (tableRotation * offsets[i]);
    8.  
    9.     }else{
    10.  
    11.       Quaternion tableRotation = Quaternion.Euler(0f, Time.time * -turnSpeed, 0f);
    12.  
    13.        planes[i].localPosition = (tableRotation * offsets[i]);
    14.  
    15. }
    16.  
    17.  
    18.  
    19. }
    20.  
    21.  
    22.  
     
    Last edited: Feb 12, 2016
  16. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    I stuck this on start just to see if it glitches when the game starts and It doesn't.
    So nothing wrong with the pictures current starting point and their setting.



    Code (csharp):
    1.  
    2. void Start(){
    3.    for(int i = 0; i < planes.Length; i++){
    4.  
    5.       Quaternion tableRotation = Quaternion.Euler(0f, Time.time * turnSpeed, 0f);
    6.  
    7.        planes[i].localPosition = (tableRotation * offsets[i]);
    8.  
    9.     }
    10.  
    11. }
    12.  
    13.  
     
  17. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Hmm that's odd
    If I get rid of the left and right statement the issue goes away.
    Does it have something to do with me telling it to move when the frame isn't ready or something?

    Code (csharp):
    1. If(TurnTableDirection == "Left") {
    2.  
    3. }
     
  18. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    It seems using deltaTime may fix this issue.
    after changing it to deltaTime. Now when I click left and right it starts from the position it should except now it only moves for one notch and doesn't continue to rotate.
    So now I just need to figure out why it doesn't continue to rotate



    Code (csharp):
    1. Quaternion tableRotation = Quaternion.Euler(0f, Time.deltaTime * turnSpeed, 0f);
     
    Last edited: Feb 12, 2016
  19. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Code (csharp):
    1. Quaternion tableRotation = Quaternion.Euler(0f, Time.deltaTime * turnSpeed, 0f);
    [/QUOTE]

    It does look like it's moving but only a little so maybe, change 0f to 1.2f and turnSpeed set to 5.0f then pump it up to match the speed you want, other than that... your code should work :/ or even better, do an animation and call the left > right animation when clicked. ;)
     
  20. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Thanks for the response Josen
    Hmm I don't think its moving lol.
    I changed the turn speed to 100000000 and it still does it.
    Also I double check and it is looping. hehe.
    changing the other 0f to 1.2 messes with the wrong rotation.

    I honestly haven't even messed with Animation tools yet in Unity.
    You probably talking about timeframe animation in unity right?



    Code (csharp):
    1.  
    2. publicTransform[] planes;
    3.  
    4. publicint turnSpeed = 10; //how fast the turntable spins (in degrees)
    5.  
    6. string TurnTableDirection = "Stop";
    7.  
    8.  
    9.  
    10. public Vector3[] offsets = newVector3[4];
    11.  
    12.  
    13. void Update(){
    14.  
    15.    if(TurnTableDirection != "Stop") {
    16.  
    17.       for(int i = 0; i < planes.Length; i++){
    18.  
    19.  
    20.  
    21.          if(TurnTableDirection == "Left") {
    22.  
    23.             Quaternion tableRotation = Quaternion.Euler(0f, Time.deltaTime * turnSpeed, 0f);
    24.  
    25.                planes[i].localPosition = (tableRotation * offsets[i]);
    26.  
    27.           }else{
    28.  
    29.              Quaternion tableRotation = Quaternion.Euler(0f, Time.deltaTime * -turnSpeed, 0f);
    30.  
    31.              planes[i].localPosition = (tableRotation * offsets[i]);
    32.  
    33.          }
    34.  
    35.     }
    36.  
    37. }
    38.  
    39. }
    40.  
    41.  
     
  21. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Right now on every loop the new position never changes
    (0.0, 0.0, 0.0, 1.0)
    So that's why its only moving once

    Code (csharp):
    1.  
    2. Quaternion tableRotation = Quaternion.Euler(0f, Time.deltaTime * -turnSpeed, 0f);
    3.  
    4. planes[i].localPosition = (tableRotation * offsets[i]);
    5.  
    6. Debug.Log(tableRotation); // result is always (0.0, 0.0, 0.0, 1.0)
    7.  
     
  22. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    I got it to rotate by subtracting deltaTime from time
    but it glitches when it first starts a hair
    After searching around. It seems delta uses the time it took to do the last frame
    so I think I need to use the difference between two times or something lol.

    Code (csharp):
    1.  
    2. Quaternion tableRotation = Quaternion.Euler(0f, Time.time - Time.deltaTime * -turnSpeed, 0f);
    3.  
    4. planes[i].localPosition = (tableRotation * offsets[i]);
    5.  
    6.  
     
  23. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Wait glitch is gone now.
    I forgot to make sure the math was being done in the right order lmao

    Code (csharp):
    1.  
    2. Quaternion tableRotation = Quaternion.Euler(0f, (Time.time - Time.deltaTime) * -turnSpeed, 0f);
    3.  
     
  24. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Ehh scratch that. That didn't fix the issue at all.
    Now its juts doing the skipping it did when I was just using Time.time.
    God I am so stuck on this its not even funny.
     
  25. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    I'm starting to understand what is going on.
    Right now builds up a distance based on how much time has went by since the start of game. This entire time I thought Time.time was the current time lol.

    So if I start the game and wait a minute then rotate. Then start the game again and hit rotate right away. It skips a different amount since a different amount of time has passed.

    So now I have a starting point on figuring this out.
     
  26. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Now that I understand what is going on I was able to fix it pretty fast lol.
    Probably a better way of doing this idk.
    basically I created my own timer.

    Code (csharp):
    1.  
    2. public Transform[] planes;
    3. public int turnSpeed = 10; //how fast the turntable spins (in degrees)
    4. string TurnTableDirection = "Stop";
    5. Vector3[] offsets = newVector3[4];
    6. float savedtime = 0.0f;
    7.  
    8.  
    9. void Start() {
    10.  
    11.    int x = 0;
    12.  
    13.    foreach(Transform plane in planes) {
    14.  
    15.       offsets[x] = plane.localPosition;
    16.  
    17.       x++;
    18.  
    19.    }
    20.  
    21. }
    22.  
    23.  
    24.  
    25. void Update(){
    26.  
    27.    if(TurnTableDirection != "Stop") {
    28.  
    29.    savedtime += .04f;
    30.  
    31.    for(int i = 0; i < planes.Length; i++){
    32.  
    33.  
    34.  
    35.       if(TurnTableDirection == "Left") {
    36.  
    37.          Quaternion tableRotation = Quaternion.Euler(0f, savedtime * turnSpeed, 0f);
    38.  
    39.          planes[i].localPosition = tableRotation * offsets[i];
    40.  
    41.       }else{
    42.  
    43.          Quaternion tableRotation = Quaternion.Euler(0f, savedtime * -turnSpeed, 0f);
    44.  
    45.          planes[i].localPosition = tableRotation * offsets[i];
    46.  
    47.       }
    48.  
    49.    }
    50.  
    51. }
    52.  
    53. }
    54.  
    55.  
    56.  
    57. public void TurnTableRotate(Button button){
    58.  
    59.    int x = 0;
    60.  
    61.    foreach(Transform plane in planes) {
    62.  
    63.       offsets[x] = plane.localPosition;
    64.  
    65.       x++;
    66.  
    67.    }
    68.  
    69.    savedtime = 0f;
    70.  
    71.    TurnTableDirection = button.name;
    72.  
    73. }
    74.  
    75.  
     
    Last edited: Feb 13, 2016
  27. Shadowing

    Shadowing

    Joined:
    Jan 29, 2015
    Posts:
    1,648
    Everything is going good.
    Is it possible to rotate this in a Oval shape instead of a circle. I notice the reason the one I have in my browser looks so great is cause its actually spending in a oval and not a circle.

    With me creating my own timer like that. I think it made the animation not very smooth.
    Think it needs to be done with delta time some how.
     
    Last edited: Feb 13, 2016
  28. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    You need to multiply your savedTime by Time.deltaTime, like so:

    Code (csharp):
    1.  
    2. savedtime += 2.0f * Time.deltaTime;
    3.  
    As for the circle vs. oval, you could do something such as:

    Code (csharp):
    1.  
    2. var rotatedOffset = tableRotation * offsets[I];
    3. rotatedOffset.z *= 0.7f;
    4. planes[i].localPosition = rotatedOffset;
    5.