[英]Change displacement in C# with keyboard?
How to change the displacement of a gameObject with a keyboard press in C# for unity? 如何在C#中使用键盘按下统一更改gameObject的位移? I tried using this, but nothing happens:
我尝试使用此方法,但没有任何反应:
if (Input.GetKey(KeyCode.W))
platform[i, j].transform.position =
new Vector3(platform[i, j].transform.position.x,
y *= y,
platform[i, j].transform.position.z);
很好,您的代码似乎还可以,但是,如果您的“ y”为1或0,则y * = y将无法仅将gameObject移至Y轴。
Use the Translate method on your transform: 在转换中使用Translate方法:
if (Input.GetKey(KeyCode.W))
platform[i, j].transform.Translate(0,1,0);
This sample will move the object upwards very fast. 该样本将使物体快速向上移动。 You can use
platform[i, j].transform.forward
as the vector if you want forward movement. 如果要向前移动
platform[i, j].transform.forward
可以使用platform[i, j].transform.forward
作为向量。
See the docs on Translate . 请参阅有关翻译的文档。 Note the
relativeTo
parameter as well. 还要注意
relativeTo
参数。 Useful for local vs world space movement. 对于本地空间与世界空间运动很有用。 Also take note of the usage of deltaTime in the samples.
还要注意样本中deltaTime的用法。
Also take note of @csblo's comment to put this code in the Update()
method. 还请注意@csblo的注释,以将该代码放入
Update()
方法中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.