UnityAPI手册-2019.4官网版
文集大纲加载中……
本文档使用 MrDoc 发布
-
+
首页
Vector3.x
# [Vector3](https://docs.unity3d.com/cn/2019.4/ScriptReference/Vector3.html).x public float **x** ; ## 描述 向量的 X 分量。 用于修改或返回向量的 X 分量。 ``` // This script moves a GameObject to a new x position using Vector3.x. // Attach this script to a GameObject // Set your x position in the Inspector using UnityEngine; public class Examples : MonoBehaviour { Vector3 m_NewPosition; // his is the new X position. Set it in the Inspector. public float m_XPosition; // Use this for initialization void Start() { // Initialise the vector m_NewPosition = new Vector3(0.0f, 0.0f, 0.0f); } void Update() { // Press the space key to change the x component of the vector if (Input.GetKeyDown(KeyCode.Space)) { m_NewPosition.x = m_XPosition; } // Change the position depending on the vector transform.position = m_NewPosition; } } ``` 下面是另一个演示如何操作 GameObject 的 x 和 y 位置的示例。只需在播放模式的输入字段中键入 x 和 y 位置。 ``` // Attach this script to a GameObject // Create two Input Fields in the Scene (Create>UI>Input Field) // Click on the GameObject and attach both Input Fields in the Inspector window using UnityEngine; using UnityEngine.UI; public class ExampleClass : MonoBehaviour { Vector3 m_NewPosition; // Attach these in the Inspector window public InputField m_InputFieldX, m_InputFieldY; // These are the strings that are returned from the InputFields string xString, yString; // These are used when converting the strings to floats float m_XValue, m_YValue; // Use this for initialization void Start() { // Initialise the vector m_NewPosition = new Vector3(0.0f, 0.0f, 0.0f); } void Update() { // Store the inputs from the InputFields as strings xString = m_InputFieldX.text; yString = m_InputFieldY.text; // Convert the strings from the InputFields to floats ConvertStringsToFloats(xString, yString); // Change the NewPosition Vector's x and y components m_NewPosition.x = m_XValue; m_NewPosition.y = m_YValue; // Change the position depending on the vector transform.position = m_NewPosition; } void ConvertStringsToFloats(string XVal, string YVal) { try { // Convert the strings to floats m_XValue = float.Parse(XVal); m_YValue = float.Parse(YVal); } catch{} } } ```
da
2022年6月22日 10:53
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
Markdown文件
分享
链接
类型
密码
更新密码