Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help with a script.

Discussion in 'Scripting' started by gamerant, Jul 21, 2013.

  1. gamerant

    gamerant

    Joined:
    Jun 27, 2013
    Posts:
    16
    Hi guys.The thing is I found a script on the forums which is perfect for my game.The problem however is that due to how it is written the game object that the script is attached to rotates by 90 degrees automatically before any input is detected. What I want to do is have the game object rotate only when input is detected.
    I'm not much of a coder but I had the script tweaked so it worked how I wanted it before all my data was lost.Since then I've been at this script for about a month and I haven't gotten anywhere with it at all.

    Here is the link to the forum page where i found the script.It's in c# and done by Jesse Anders. http://forum.unity3d.com/threads/86174-Rotating-object-on-2-axis

    And here is part of the script that causes this.In the script the line of code
    Code (csharp):
    1. transform.rotation = orientations[orientationIndex];
    is what sets the rotation of the object at the beginning. If i comment it out or change it slightly then the game object starts doing crazy rotations.This is as far as I managed to get in this entire time sadly.

    Code (csharp):
    1. void Start()
    2. {
    3.         Quaternion[] rotations = new Quaternion[] {
    4.  
    5.             Quaternion.AngleAxis(-90, Vector3.up),
    6.             Quaternion.AngleAxis(90, Vector3.up),
    7.             Quaternion.AngleAxis(-90, Vector3.right),
    8.             Quaternion.AngleAxis(90, Vector3.right)
    9.         };
    10.  
    11.         for (int i = 0; i < 24; ++i)
    12.         {
    13.  
    14.             for (int j = 0; j < 4; ++j)
    15.             {
    16.  
    17.                 var currentOrientation = rotations[j] * orientations[i];
    18.  
    19.                 float maxDot = 0f;
    20.  
    21.                 int neighborIndex = 0;
    22.  
    23.                 for (int k = 0; k < 24; ++k)
    24.                 {
    25.  
    26.                     float dot = Mathf.Abs(Quaternion.Dot(currentOrientation, orientations[k]));
    27.  
    28.                     if (dot > maxDot)
    29.                     {
    30.                         maxDot = dot;
    31.                         neighborIndex = k;
    32.                     }
    33.                 }
    34.                neighborIndices[i, j] = neighborIndex;
    35.             }
    36.         }
    37.  
    38.       transform.rotation = orientations[orientationIndex];
    39.     }
    Any help with the script would be appreciated.That includes hints and tips :razz:
    Thanks in advance.
    Peace!
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Theres no array there called "orientations", but you use it several times
    And aside from that, you havent said what its supposed to do, or what you are trying to make it do
     
  3. gamerant

    gamerant

    Joined:
    Jun 27, 2013
    Posts:
    16
    I thought I explained it there.My bad.
    The array is declared, but it's huge hence i didn't post it.

    Thank you for the interest but I tried a different script and it works :) So i think i'll stick with it.