I know Transform is used to get position, rotation and scale of an gameobject but what does transform do?
{
public Transform player;
private void Update()
{
transform.position = player.position;
}
}
here Transform gets the position, rotation and scale and then stores it in the variable player. I also understand that from the line "transform.position" that it is used to convert the position to player position but is that the only use and am I right about this?
CodePudding user response:
Transform
is a type, and is described in the Unity Documentation as
Position, rotation and scale of an object. Every object in a Scene has a Transform. It's used to store and manipulate the position, rotation and scale of the object. Every Transform can have a parent, which allows you to apply position, rotation and scale hierarchically. This is the hierarchy seen in the Hierarchy pane.
Every C# script that inherits MonoBehaviour
in Unity will have a transform. Inherting MonoBehaviour
will allow for a script to be attached to a GameObject
in Unity. The MonoBehaviour
class has many fields, one of them is the transform
field, which has the type of Transform
. The Unity Documentation describes the field as
The Transform attached to this GameObject.
Every GameObject
has a Transform
component, meaning that every Unity script using MonoBehaviour
will have a non-null field called transform.
If your script is attached to the player GameObject
, then there would be no need for you to store a reference to the player, and could instead use the transform variable.
CodePudding user response:
Transform
is a class in UnityEngine while transform
is a property of type Transform
from the GameObject
attached to the script.