Legacy Documentation: Version 2018.1 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Transform.position

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public var position: Vector3;
public Vector3 position;

Description

The position of the transform in world space.

The position member can be accessed by the Game code. Setting this value can be used to animate the GameObject. The example below makes an attached sphere bounce by updating the position. This bouncing slowly comes to an end. The position can also be use to determine where in 3D space the transform is located.

#pragma strict
// Use Transform.position to bounce a sphere.
// The sphere and a quad are colored using materials.
public class ExampleScript extends MonoBehaviour {
	var velocity: Vector3 = new Vector3(0.0f, 1.0f, 0.0f);
	var floorHeight: float = 0.0f;
	var sleepThreshold: float = 0.05f;
	var gravity: float = -9.8f;
	function Start() {
		transform.position = new Vector3(0.0f, 1.5f, 0.0f);
	}
	function FixedUpdate() {
		if (velocity.magnitude > sleepThreshold || transform.position.y > floorHeight) {
			velocity += new Vector3(0.0f, gravity * Time.fixedDeltaTime, 0.0f);
		}
		transform.position += velocity * Time.fixedDeltaTime;
		if (transform.position.y <= floorHeight) {
			transform.position = new Vector3(0.0f, floorHeight, 0.0f);
			velocity.y = -velocity.y;
		}
	}
}
using UnityEngine;

// Use Transform.position to bounce a sphere. // The sphere and a quad are colored using materials.

public class ExampleScript : MonoBehaviour { Vector3 velocity = new Vector3(0.0f, 1.0f, 0.0f); float floorHeight = 0.0f; float sleepThreshold = 0.05f; float gravity = -9.8f;

void Start() { transform.position = new Vector3(0.0f, 1.5f, 0.0f); }

void FixedUpdate() { if (velocity.magnitude > sleepThreshold || transform.position.y > floorHeight) { velocity += new Vector3(0.0f, gravity * Time.fixedDeltaTime, 0.0f); }

transform.position += velocity * Time.fixedDeltaTime; if (transform.position.y <= floorHeight) { transform.position = new Vector3(0.0f, floorHeight, 0.0f); velocity.y = -velocity.y; } } }
对文档有任何疑问,请移步至开发者社区提问,我们将尽快为您解答