Good day, dear reader! Habré has repeatedly published articles on game development using the remarkable
Unity3d engine. Most of these articles were devoted to quite specific tasks, but I wanted to make a general excursion into this engine. This part will be devoted to the most frequently used scripting methods and objects that are used by me in the development process on this engine. I will give examples in JavaScript, as in the language closest to me.
Game object
Create a new game object called MyObject.
var myObject = new GameObject("MyObject");
The created object will be accessible via the link myObject.
Search for an object by its name.
var myObject = GameObject.Find("MyObject");
Tags can be used to mark groups of objects with similar properties, or used in a single scene.
Search object by tag, returns a single object:
var myObject = GameObject.FindWithTag("MyTag");
')
Returns a list of all objects with the specified tag:
var objectList = GameObject.FindGameObjectsWithTag("MyTag");
Check that the object has the required tag. Returns true if the specified object has a MyTag tag:
var isCompare = GameObject.CompareTag("MyTag");
Destruction of the object:
Destroy(myObject);
Destruction of an object in a minute after its creation:
Destroy(myObject, 60);
Returns the component component associated with the GameObject object, or null if the object does not contain this component. It can be used, for example, to access other scripts bound to an object.
var objectComponent = GameObject.GetComponent(component);
Returns all components of the componentType type.
var objectComponents = GameObject.GetComponents(componentType);
Bind the myComponent component to a GameObject object and get a reference to it.
var component = GameObject.AddComponent(myComponent);
The position of the game object
The transform property of the GameObject object contains information about the position of the object in the game world.
Returns the global coordinates of an object in the game world. The return value is of type Vector3, which is a list of 3 coordinates - x, y and z:
var position = GameObject.transform.position; var x = position.x;
Move the object to the point 0, 10, 0 of the game world.
GameObject.transform.position = Vector3(0, 10, 0);
The same as in the case of global coordinates, but with local ones. Local coordinates are relative to the parent object. In the case of the absence of the parent object, the local coordinates coincide with the global ones:
var localPosition = GameObject.transform.localPosition; var x = localPosition.x;
Rotate the object in the
corners of Euler . The method also returns coordinates as a Vector3 object:
var eulerAngle = GameObject.transform.eulerAngles;
The same as the previous example, but the rotation of the object is calculated relative to the parent object:
var localEulerAngle = GameObject.transform.localEulerAngles;
The current angle of rotation of the object, based on
quaternions . Returns an object of type Quaternion.
var quaternionAngle = GameObject.transform.rotation;
The current rotation of the object, based on quaternions, but relative to the parent object:
var localQuaternionAngle = GameObject.transform.localRotation;
Reset the angle of rotation of the object:
GameObject.transform.rotation = Quaternion.identity; GameObject.transform.localRotation = Quaternion.identity;
Rotate our object in the specified direction at a speed of 1 degree per second. Accepts an object of type Vector3 as coordinates. The deltaTime method of the Time object contains the time in seconds spent on the execution of the previous frame:
GameObject.transform.Rotate(Vector3.left * Time.deltaTime);
The same as the previous example, but the rotation of the object relative to the coordinates of the parent:
GameObject.transform.localRotate(Vector3.left * Time.deltaTime);
We move our object in the specified direction at a speed of 1 unit per second. Also takes as a coordinate an object of class Vector3:
GameObject.transform.Translate(Vector3.up * Time.deltaTime);
Physical properties of the game object
The rigidObody method of the GameObject object stores its physical properties. Before using the rigidbody method, it must be added to the game object.
Get / set the object velocity vector:
var velocity = GameObject.rigidbody.velocity; GameObject.rigidbody.velocity = Vector3(0, 1, 0);
The force of the counteraction of the object. It can be used to slow down speed in an environment with no friction force. Most often used to slow down falling objects, for example when creating a parachute. Accepts an integer as a parameter:
GameObject.rigidbody.drag = 100;
Set the mass of the object. It is recommended to use a mass in the range from 0.1 to 10. Using too large values can lead to unpredictable results when calculating physics:
GameObject.rigidbody.mass = 5;
Effect on the object of gravity. Accepts a boolean value as a parameter. Allows you to disable the effect of gravity on individual objects:
GameObject.rigidbody.useGravity = false;
The influence of physics on the game object. Allows you to disable partly or completely the influence of physical laws on the object:
GameObject.rigidbody.isKinematic = true;
The prohibition of the rotation of the object. Most often used when it is necessary to maintain a certain angle of rotation, even after colliding with other objects:
GameObject.rigidbody.freezeRotation = true;
Specify the coordinates of the center of mass of the object. Applies coordinates in the form of a Vector3 object already familiar to us.
GameObject.rigidbody.centerOfMass = Vector3(1, 0, 0);
Whether to use object collision detection with other objects. You can turn it off, then your object will ignore any collisions:
GameObject.rigidbody.detectCollisions = false;
The mode of determining collisions between objects. You can specify several different modes:
CollisionDetectionMode.ContinuousDynamic for fast moving objects;
CollisionDetectionMode.Continuous for collisions with fast moving objects;
CollisionDetectionMode.Discrete (default) for normal collisions;
If there are no problems with collision detection, it is recommended to use the default property.
Set the object density:
GameObject.rigidbody.SetDensity(1.5);
Apply a pulse to an object with the specified vector. As a result of the impulse application, the object will move in proportion to the impulse force.
GameObject.rigidbody.AddForce(5, 0, 0);
Apply momentum to an object with a vector in its (object) coordinate system:
GameObject.rigidbody.AddRelativeForce(0, 0, 5);
Add torque to the object. Using this method will cause the object to rotate around its center of mass GameObject.rigidbody.centerOfMass.
GameObject.rigidbody.AddTorque(0, 1, 0);
Same as the previous example, but relative to the coordinates of the object:
GameObject.rigidbody.AddRelativeTorque (1, 0, 0);
Apply a pulse to an object from an external specified point. Makes an object move and rotate at the same time. It can be used, for example, to simulate a bullet hit. The first parameter indicates the direction vector of the force, the second parameter indicates the starting point of the direction of the force.
GameObject.rigidbody.AddForceAtPosition(Vector3(0, 5, 7), Bomb.transform.position);
For full simulation of volumetric explosions in Unity3D there is a separate method. The first parameter of the method allows you to specify the pulse power, the second parameter is the point from which the pulse originates, the third parameter is the radius of pulse propagation, the fourth parameter is the compression modifier of the force propagation sphere, the fifth, optional, parameter indicates the type of pulse used:
GameObject.rigidbody.AddExplosionForce(power, explosionPos, radius, 2.0);
To make the object “fall asleep”, and prohibit further calculation of physical indicators for it:
GameObject.rigidbody.Sleep();
Check if the object has fallen asleep:
GameObject.rigidbody.IsSleeping();
"Wake up" the object for the possibility of further application of the influence of physics on it:
GameObject.rigidbody.WakeUp();
Ray tracing
One of the most commonly used in the development of the Unity3D object is Ray. This object allows you to release the beam from a specified point in a specified direction, and return some properties of objects that it was able to achieve.
Create an object of the RaycastHit class that contains information about the object that the ray encountered:
var hit : RaycastHit;
Send a ray of 50 units in length from the rayPosition position in the direction of the rayVector, and put the object that the ray encountered in the hit variable:
Physics.Raycast(rayPosition, rayVector.forward, hit, 50);
Get the distance to the object with which the beam collided. The distance can not be greater than the length of the beam:
var distance = hit.distance;
Sometimes it is necessary to obtain the name of the object with which the beam collided. The easiest way to do this:
var objectName = hit.collider.gameObject.name;
To obtain an object tag, use the following method:
var Tag = hit.collider.tag;
Unity3D contains many more different methods and objects that are not very useful. Unfortunately, a full review of them would have increased the already voluminous article, so I will try to tell you about the rest in more detail in the future, if I have the opportunity. I would like to wish experienced developers more interesting projects, and to beginners - success and interesting discoveries. Thank you for paying attention to this article.
UPD.
second part (script events).