以下运行脚本挂在“Image”上
属性(成员变量)
position:transform.position,世界坐标系中的位置,绝对坐标。
localPosition:transform.localPosition,相对于父对象的位置,相对坐标,图中的1的x、y、z既是该对象的相对坐标。如果一个物体没有父对象,二者等效。
例:做垂直或水平匀速移动
private float movementSpeed = 5f;
void Update()
{
//get the Input from Horizontal axis
float horizontalInput = Input.GetAxis("Horizontal");
//get the Input from Vertical axis
float verticalInput = Input.GetAxis("Vertical");
//update the position
transform.position = transform.position + new Vector3(horizontalInput * movementSpeed * Time.deltaTime, verticalInput * movementSpeed * Time.deltaTime, 0);
//output to log the position change
Debug.Log(transform.position);
}
eulerAngles:世界坐标系中的欧拉角(以度为单位)。transform.eulerAngle = new Vector3(0.0f,0.0f,0.0f),围绕x、y、z轴做旋转。
localEulerAngles:相对于父级的变换欧拉角。
例:围绕x、y、z轴做旋转
float rotationSpeed = 45;
Vector3 currentEulerAngles;
float x;
float y;
float z;
void Update()
{
if (Input.GetKeyDown(KeyCode.X)) x = 1 - x;
if (Input.GetKeyDown(KeyCode.Y)) y = 1 - y;
if (Input.GetKeyDown(KeyCode.Z)) z = 1 - z;
//modifying the Vector3, based on input multiplied by speed and time
currentEulerAngles += new Vector3(x, y, z) * Time.deltaTime * rotationSpeed;
//apply the change to the gameObject
transform.eulerAngles = currentEulerAngles;
}
rotation:世界坐标系中的旋转(四元数)。transform.rotation=Quaternion.Euler(0.0f,0.0f,0.0f);在世界坐标系中物体变换的旋转角度作为Quaternion