Lef*_*ler 5 c# unity-game-engine
我正在使用Unity开始我的开发.我正在做那样的事情:
if(Input.GetKey(KeyCode.A))newValues[1]-=this.turnSpeed*(float)Time.deltaTime;
if(Input.GetKey(KeyCode.D))newValues[1]+=this.turnSpeed*(float)Time.deltaTime;
transform.Rotate(0, newValues[1], 0);
if(Input.GetKey(KeyCode.W))newValues[0]+=this.speed*transform.forward.z*(float)Time.deltaTime;
if(Input.GetKey(KeyCode.S))newValues[0]-=this.speed*transform.forward.z*(float)Time.deltaTime;
transform.position = new Vector3(transform.position.x, transform.position.y, (float)newValues[0]);
Run Code Online (Sandbox Code Playgroud)
所以我旋转,我可以移动,但它只是在Z线移动,我知道我正在调用Z特定的运动.但使用Javascript我可以做点什么
transform.forward+=this.speed*transform.forward*(float)Time.deltaTime;
Run Code Online (Sandbox Code Playgroud)
因此,我不需要执行新的矢量处理并复制到单独的变量,它就像一个魅力,使用旋转并在旋转时使用自己作为方向.
你可能会误解transform.forward的用法. transform.forward只是一个向量来告诉你gameObject面向的方向,它取决于transform.rotation.
如果要移动GameObject,请始终使用transform.position:
transform.position += this.speed * transform.forward * (float)Time.deltaTime;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9278 次 |
| 最近记录: |