Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

When do we use new Vector3?

Discussion in 'Scripting' started by Rutenis, Jun 24, 2014.

  1. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    Hey, whenever i write a code, i always think why do i have to use new Vector3 in cSharp? Can anyone explane it to me? Thanks! :) :D
     
  2. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
    Magiichan likes this.
  3. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    All the time when we have a position or a direction.

    Well, there's even a whole class in school named "Vectoriel Mathematics"
     
  4. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    to call upon the constructor.
    Vector3 in example is a struct so looking a bit like this (assuming pro coders will correct me if I'm wrong):
    Code (CSharp):
    1. public struct Vector3
    2. {
    3.     public float x, y, z;
    4.  
    5.     public Vector3(float X, float Y, float Z)
    6.     {
    7.         x = X;
    8.         y = Y;
    9.         z = Z;
    10.     }
    11. }
    so to create a new instance of the Vector3 struct, we call upon it's constructor like Vector3 v3 = new Vector3(1, 1, 1);
    where 1, 1, 1 are the X, Y, Z values.
     
    Magiichan likes this.
  5. GZMRD17

    GZMRD17

    Joined:
    Jan 25, 2020
    Posts:
    33
    Can I treat vector3 from unity like vector3 in standard C# math libraries? I have an MS kinect application where I have some old code that another person wrote and uses vector3 from C# libraries and now I may need to add a vector3 from unity. Is there any difference mathematically from either one or both have X, Y, Z components? Can I use them interchangeably?
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    If it's from a different library, they're technically different types. So unfortunately they're not technically interchangeable despite being the same shape.

    You'd have to write a conversion method for them... here's a simple one done like an extension method:
    Code (csharp):
    1.  
    2.     public static class VectorConverter
    3.     {
    4.         public static UnityEngine.Vector3 ToUnityVector3(this OtherLib.Vector3 v)
    5.         {
    6.             return new UnityEngine.Vector3()
    7.             {
    8.                 x = v.x,
    9.                 y = v.y,
    10.                 z = v.z
    11.             };
    12.         }
    13.  
    14.         public static OtherLib.Vector3 ToOtherLibVector3(this UnityEngine.Vector3 v)
    15.         {
    16.             return new OtherLib.Vector3()
    17.             {
    18.                 x = v.x,
    19.                 y = v.y,
    20.                 z = v.z
    21.             };
    22.         }
    23.     }
    24.  
    (names are generalized as I don't know what library you're talking about specifically)

    ...

    If you're just taking generalized code and tossing out the old library (porting it). Then you can just use the UnityEngine.Vector3. You've ported the code, and as long as the 2 vector types behave similarly (they likely do... Vector3 is based on the math concept of them which is fairly standardized... they're not unique to Unity).
     
  7. GZMRD17

    GZMRD17

    Joined:
    Jan 25, 2020
    Posts:
    33
    I really appreciate it. The library I am using is: System.Numerics;
     
  8. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    it's practically the same, it also uses 32-bit floats for components. use lordofduct's code for conversion on the fly, it's perfect for what you need.
     
  9. GZMRD17

    GZMRD17

    Joined:
    Jan 25, 2020
    Posts:
    33
    Thanks will check it out.