Moving Objects in Unity: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
TomasM (Diskussion | Beiträge) Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
Zeile 3: | Zeile 3: | ||
There are different ways to move an object within 3D space with code. Here we go over 5 options: | There are different ways to move an object within 3D space with code. Here we go over 5 options: | ||
=== A - Transform.position === | |||
'''Transform:''' a Class which contains the information of position, rotation and scale of an object. | |||
'''transform.position:''' The position property of a GameObject's Transform. Assigns the object a new position. | |||
'''Vector:''' A Class refering to a mathematical entity commonly drewn as an arrow in two or three dimensional space, which has a a magnitude (size or length) and a direction (angle θ). | |||
'''Vector3:''' is a Unity C# struct with given x, y & z components, representing points and vectors in 3D space. In other words, a group of Vectors. | |||
example 1) | |||
private void Update() | |||
{ | |||
transform.position += transform.forward * Time.deltaTime; | |||
} | |||
* the “f” is there to state that the variable 0.01 is of type float. | |||
example 2) | |||
void Update() | |||
{ | |||
transform.position += new Vector3(0, 0, 0.01f); | |||
} | |||
*Vector3.forward is the unit vector defined by (0, 0, 1) | |||
*Time.deltaTime is a variable used so velocity is consistent across different frame rates. | |||
[[B) transform.Translate]] | [[B) transform.Translate]] |
Version vom 2. März 2021, 09:18 Uhr
Unity : How to move a GameObject in 3D space?
There are different ways to move an object within 3D space with code. Here we go over 5 options:
A - Transform.position
Transform: a Class which contains the information of position, rotation and scale of an object.
transform.position: The position property of a GameObject's Transform. Assigns the object a new position.
Vector: A Class refering to a mathematical entity commonly drewn as an arrow in two or three dimensional space, which has a a magnitude (size or length) and a direction (angle θ).
Vector3: is a Unity C# struct with given x, y & z components, representing points and vectors in 3D space. In other words, a group of Vectors.
example 1)
private void Update() { transform.position += transform.forward * Time.deltaTime; }
- the “f” is there to state that the variable 0.01 is of type float.
example 2)
void Update() { transform.position += new Vector3(0, 0, 0.01f); }
- Vector3.forward is the unit vector defined by (0, 0, 1)
- Time.deltaTime is a variable used so velocity is consistent across different frame rates.