1

您好,我遵循了有关如何制作自上而下射击游戏的教程,代码使我的角色对着鼠标旋转,并且我可以使用 W 和 S 前后移动。但是我也希望能够使用左右移动当涉及到 javascript 和 unity3d 中的代码时,A 和 D 我都不好,我的大部分编码都是在 c# 中完成的。制作教程的人解释得很差,其中一些代码实际上做了什么。

这是代码:

#pragma strict

var speed : float = 20.0;
var rotateSpeed : float = 2.0;

function Update () {

var controller : CharacterController = GetComponent(CharacterController);
transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed,0);

var forward : Vector3 = transform.TransformDirection(Vector3.forward);
var curSpeed : float = speed * Input.GetAxis("Vertical");
controller.SimpleMove(forward * curSpeed);

var position = Input.mousePosition;
var newposition = Vector3(position.x,position.y,camera.main.transform.position.y- transform.position.y);
var lastposition = camera.main.ScreenToWorldPoint(newposition);
transform.LookAt(lastposition);

}

@script RequireComponent(CharacterController)
4

1 回答 1

1

人员配备代码(不转左右移动)

if(Input.GetKeyUp("d")){ 
    controller.SimpleMove(right * staffingSpeed);
}
else if(Input.GetKeyUp("a")) {
    controller.SimpleMove(-right * staffingSpeed);
}

在 update() 函数中编写代码并在函数外部创建一个变量“staffingSpeed”并从检查器设置它或将“staffingSpeed”替换为常数数值,例如 5

于 2013-07-01T19:25:51.577 回答