6
6
7
7
8
8
import { initializeModule , getInstance , type GameObjectIdentifier } from './UnityAcademy' ;
9
+ import {
10
+ type Vector3 , checkVector3Parameter , makeVector3D , scaleVector , addVector , dotProduct , crossProduct ,
11
+ normalizeVector , vectorMagnitude , zeroVector , pointDistance ,
12
+ } from './UnityAcademyMaths' ;
9
13
10
14
11
15
/**
12
- * Load and initialize Unity Academy WebGL player and set it to 2D mode. All other functions in this module requires calling this function or init_unity_academy_3d first.<br>
16
+ * Load and initialize Unity Academy WebGL player and set it to 2D mode. All other functions (except Maths functions) in this module requires calling this function or init_unity_academy_3d first.<br>
13
17
* I recommand you just call this function at the beginning of your Source Unity program under the 'import' statements.
14
18
*
15
19
* @category Application Initialization
@@ -20,7 +24,7 @@ export function init_unity_academy_2d() : void {
20
24
}
21
25
22
26
/**
23
- * Load and initialize Unity Academy WebGL player and set it to 3D mode. All other functions in this module requires calling this function or init_unity_academy_2d first.<br>
27
+ * Load and initialize Unity Academy WebGL player and set it to 3D mode. All other functions (except Maths functions) in this module requires calling this function or init_unity_academy_2d first.<br>
24
28
* I recommand you just call this function at the beginning of your Source Unity program under the 'import' statements.
25
29
*
26
30
* @category Application Initialization
@@ -459,6 +463,49 @@ export function copy_scale(from : GameObjectIdentifier, to : GameObjectIdentifie
459
463
. copyTransformPropertiesInternal ( 'scale' , from , to , delta_x , delta_y , delta_z ) ;
460
464
}
461
465
466
+ /**
467
+ * Makes the GameObject "Look At" a given position.<br>
468
+ * <b>3D mode only</b><br>
469
+ * <br>
470
+ * The +Z direction of the GameObject (which denotes forward in Unity's conventions) will pointing to the given position.<br>
471
+ * <br>
472
+ * For more information, see https://docs.unity3d.com/ScriptReference/Transform.LookAt.html<br>
473
+ *
474
+ * @param gameObjectIdentifier The identifier for the GameObject that you need to make it "look at" a position
475
+ * @param x The X value of the "look at" position
476
+ * @param y The Y value of the "look at" position
477
+ * @param z The Z value of the "look at" position
478
+ *
479
+ * @category Transform
480
+ */
481
+ export function look_at ( gameObjectIdentifier : GameObjectIdentifier , x : number , y : number , z : number ) : void {
482
+ checkUnityAcademyExistence ( ) ;
483
+ checkIs3DMode ( ) ;
484
+ checkGameObjectIdentifierParameter ( gameObjectIdentifier ) ;
485
+ checkParameterType ( x , 'number' ) ;
486
+ checkParameterType ( y , 'number' ) ;
487
+ checkParameterType ( z , 'number' ) ;
488
+ getInstance ( )
489
+ . lookAtPositionInternal ( gameObjectIdentifier , x , y , z ) ;
490
+ }
491
+
492
+ /**
493
+ * Calcuate the distance between two GameObjects, based on each other's position
494
+ * @param gameObjectIdentifier_A The identifier for the first GameObject
495
+ * @param gameObjectIdentifier_B The identifier for the second GameObject
496
+ *
497
+ *
498
+ * @return The value of the distance between these two GameObjects
499
+ * @category Transform
500
+ */
501
+ export function gameobject_distance ( gameObjectIdentifier_A : GameObjectIdentifier , gameObjectIdentifier_B : GameObjectIdentifier ) : number {
502
+ checkUnityAcademyExistence ( ) ;
503
+ checkGameObjectIdentifierParameter ( gameObjectIdentifier_A ) ;
504
+ checkGameObjectIdentifierParameter ( gameObjectIdentifier_B ) ;
505
+ return getInstance ( )
506
+ . gameObjectDistanceInternal ( gameObjectIdentifier_A , gameObjectIdentifier_B ) ;
507
+ }
508
+
462
509
function checkKeyCodeValidityAndToLowerCase ( keyCode : string ) : string {
463
510
if ( typeof ( keyCode ) !== 'string' ) throw new Error ( `Key code must be a string! Given type: ${ typeof ( keyCode ) } ` ) ;
464
511
if ( keyCode === 'LeftMouseBtn' || keyCode === 'RightMouseBtn' || keyCode === 'MiddleMouseBtn' || keyCode === 'LeftShift' || keyCode === 'RightShift' ) return keyCode ;
@@ -940,3 +987,165 @@ export function get_custom_prop(gameObjectIdentifier : GameObjectIdentifier, pro
940
987
return getInstance ( )
941
988
. getCustomPropertyInternal ( gameObjectIdentifier , propName ) ;
942
989
}
990
+
991
+ /**
992
+ * Create a 3D vector
993
+ * @param x The x component of the new vector
994
+ * @param y The y component of the new vector
995
+ * @param z The z component of the new vector
996
+ *
997
+ * @return The 3D vector (x, y, z)
998
+ *
999
+ * @category Maths
1000
+ */
1001
+ export function vector3 ( x : number , y : number , z : number ) : Vector3 {
1002
+ checkParameterType ( x , 'number' ) ;
1003
+ checkParameterType ( y , 'number' ) ;
1004
+ checkParameterType ( z , 'number' ) ;
1005
+ return makeVector3D ( x , y , z ) ;
1006
+ }
1007
+
1008
+ /**
1009
+ * Get the X component of a 3D vector
1010
+ * @param vector The 3D vector
1011
+ *
1012
+ * @return The X component of the given vector
1013
+ *
1014
+ * @category Maths
1015
+ */
1016
+ export function get_x ( vector : Vector3 ) : number {
1017
+ checkVector3Parameter ( vector ) ;
1018
+ return vector . x ;
1019
+ }
1020
+
1021
+ /**
1022
+ * Get the Y component of a 3D vector
1023
+ * @param vector The 3D vector
1024
+ *
1025
+ * @return The Y component of the given vector
1026
+ *
1027
+ * @category Maths
1028
+ */
1029
+ export function get_y ( vector : Vector3 ) : number {
1030
+ checkVector3Parameter ( vector ) ;
1031
+ return vector . y ;
1032
+ }
1033
+
1034
+ /**
1035
+ * Get the Z component of a 3D vector
1036
+ * @param vector The 3D vector
1037
+ *
1038
+ * @return The Z component of the given vector
1039
+ *
1040
+ * @category Maths
1041
+ */
1042
+ export function get_z ( vector : Vector3 ) : number {
1043
+ checkVector3Parameter ( vector ) ;
1044
+ return vector . z ;
1045
+ }
1046
+
1047
+ /**
1048
+ * Scales a 3D vector with the given factor.
1049
+ * @param vector The original vector
1050
+ * @param factor The scaling factor.
1051
+ * @return The scaled vector
1052
+ *
1053
+ * @category Maths
1054
+ */
1055
+ export function scale_vector ( vector : Vector3 , factor : number ) : Vector3 {
1056
+ checkVector3Parameter ( vector ) ;
1057
+ checkParameterType ( factor , 'number' ) ;
1058
+ return scaleVector ( vector , factor ) ;
1059
+ }
1060
+
1061
+ /**
1062
+ * Add two 3D vectors together.
1063
+ * @param vectorA The first vector
1064
+ * @param vectorB The second vector.
1065
+ * @return The sum of the two vectors
1066
+ *
1067
+ * @category Maths
1068
+ */
1069
+ export function add_vector ( vectorA : Vector3 , vectorB : Vector3 ) : Vector3 {
1070
+ checkVector3Parameter ( vectorA ) ;
1071
+ checkVector3Parameter ( vectorB ) ;
1072
+ return addVector ( vectorA , vectorB ) ;
1073
+ }
1074
+
1075
+ /**
1076
+ * Calcuate the dot product of two 3D vectors.
1077
+ * @param vectorA The first vector
1078
+ * @param vectorB The second vector.
1079
+ * @return The dot product
1080
+ *
1081
+ * @category Maths
1082
+ */
1083
+ export function dot ( vectorA : Vector3 , vectorB : Vector3 ) : number {
1084
+ checkVector3Parameter ( vectorA ) ;
1085
+ checkVector3Parameter ( vectorB ) ;
1086
+ return dotProduct ( vectorA , vectorB ) ;
1087
+ }
1088
+
1089
+ /**
1090
+ * Calcuate the cross product of two 3D vectors.
1091
+ * @param vectorA The first vector
1092
+ * @param vectorB The second vector.
1093
+ * @return The cross product
1094
+ *
1095
+ * @category Maths
1096
+ */
1097
+ export function cross ( vectorA : Vector3 , vectorB : Vector3 ) : Vector3 {
1098
+ checkVector3Parameter ( vectorA ) ;
1099
+ checkVector3Parameter ( vectorB ) ;
1100
+ return crossProduct ( vectorA , vectorB ) ;
1101
+ }
1102
+
1103
+ /**
1104
+ * Normalize a vector. The returned vector will have the same direction as the original vector but have a magnitude of 1.
1105
+ * @param vector The original vector
1106
+ * @return The normalized vector. This function will return a zero vector if the original vector is a zero vector.
1107
+ *
1108
+ * @category Maths
1109
+ */
1110
+ export function normalize ( vector : Vector3 ) : Vector3 {
1111
+ checkVector3Parameter ( vector ) ;
1112
+ return normalizeVector ( vector ) ;
1113
+ }
1114
+
1115
+ /**
1116
+ * Calcuate the magnitude of a vector
1117
+ * @param vector The vector
1118
+ * @return The magnitude of the vector
1119
+ *
1120
+ * @category Maths
1121
+ */
1122
+ export function magnitude ( vector : Vector3 ) : number {
1123
+ checkVector3Parameter ( vector ) ;
1124
+ return vectorMagnitude ( vector ) ;
1125
+ }
1126
+
1127
+ /**
1128
+ * Get the zero vector
1129
+ * @return The zero vector
1130
+ *
1131
+ * @category Maths
1132
+ */
1133
+ export function zero_vector ( ) : Vector3 {
1134
+ return zeroVector ( ) ;
1135
+ }
1136
+
1137
+ /**
1138
+ * Calcuate the distance between two 3D points
1139
+ *
1140
+ * @param pointA The first point
1141
+ * @param pointB The second point
1142
+ *
1143
+ * @return The value of the distance between the two points
1144
+ *
1145
+ * @category Maths
1146
+ */
1147
+ export function point_distance ( pointA : Vector3 , pointB : Vector3 ) : number {
1148
+ checkVector3Parameter ( pointA ) ;
1149
+ checkVector3Parameter ( pointB ) ;
1150
+ return pointDistance ( pointA , pointB ) ;
1151
+ }
0 commit comments