Version: 2020.3
public Vector3 position ;

描述

世界空间中的变换位置。

GameObjectTransformposition 属性,可以在 Unity Editor 中以及通过脚本来访问该属性。变更该值可移动 GameObject。获得该值可在 3D 世界空间中定位此 GameObject

using UnityEngine;

public class ExampleClass : MonoBehaviour { //movement speed in units per second 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); } }

该示例从水平和垂直轴获取输入,然后通过更改 GameObject 的位置将它向上/向下或向左/向右移动。