Search Unity

2 Qs: scripting tutorial and rotating a vector3

Discussion in 'Scripting' started by Morgan, Jun 14, 2006.

  1. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    There are two things I'm not grasping today that are probably very simple!

    1. How can you rotate a vector3?

    For instance, I have captured an object's rigidbody.velocity, and I want to find the direction that's directly to the left of that motion. So I want to rotate the velocity vector 90 degrees around Y and get another vector3 as the result. I can probably use a bunch of atans etc. but usually when I catch myself doing that it means I'm overlooking that Unity has a simpler way :)


    2. On page 20 of the very helfpul Scripting Tutorial PDF, the script contains the following line:

    Code (csharp):
    1. private var activeWayPoint : WayPoint;
    What is being done here? Normally after the colon I would expect something like "Transform" that's in the Unity manual. "WayPoint" is not. So what, then, is WayPoint in this line?

    I know what waypoints are used for in the tutorial, but I don't get that line of code. Something similar happens with : Score in the other tutorial. I think I need the beginner's paragraph on what you can and can't do with that colon!

    Many thanks.
     
  2. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Ad1:

    Your answer to rotating vectors in Unity is Quaternion. You acheive the rotated vector by multiplying a Quaternion containing the desired rotation with the original vector:

    Code (csharp):
    1.  
    2. var rotation=Quaternion.EulerAngles(10 * Mathf.Deg2Rad, 10 * Mathf.Deg2Rad, 15 * Mathf.Deg2Rad);
    3. var rotatedVector = rotation * originalVector;
    4.  
    But to roate 90 degrees around an axis is even easier - use the cross product:
    Code (csharp):
    1.  
    2. var rotatedVector = Vector3.Cross(originalVector, Vector3.up);
    3.  
    Ad. 2:

    WayPoint is probably a user defined script. For it to work, the project must contain a script called WayPoint.js.

    You can put any type name after the colon. ie. internal types (int, float, string, ...), Unity Engine classes (GameObject, Transform, Vector3, Quaternion, ...), .Net framework classes (Hashtable, ArrayList, ...) and classes and scripts included in the project (MyJavaScript1, MyJavaScript2, WayPoint, whatever you name them)
     
  3. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    Thanks! My vector math studies from 10 years ago have been too long buried.

    I mostly understand the second answer too, but now I'm wondering, re custom classes--how and why would you create one?

    (I'm too used to other scripting methods I think, Lingo especially.)
     
  4. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Because you can't write your own code without automatically creating one. Every JS file you create is a new custom class.

    ... or to put it another way: everything in Unity is objects.

    When you create a var like this
    Code (csharp):
    1.  
    2. var other : SomeOtherScript
    3.  
    You can drag any game object containing a SomeOtherScript component onto the variable in the inspector.
     
  5. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    Thanks--I understand with scripts, but when you say "classes and scripts included in the project"... are there other user-created classes besides scripts, that would be referenced in this way?
     
  6. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Yes you can create any class you want.

    Javascript has a class keyword for defining classes that don't implicitly inherit from MonoBehaviour.

    This is useful for utility code and grouping data into one variable.
     
  7. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    Thanks. More fodder for my growing knowledge of the Unity arsenal!