我有一个2D游戏,其中GameObject(GO)移动到地板的右侧 . 我想让它跳起来 .
但是当我将 Character Controller 添加到我的GO并开始模拟时,它只是在左侧快速移动 . 可以't understand, what'错了 .
GO还包含 Rigidbody 和 Box Collider
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
void Start ()
{
player = (GameObject)this.gameObject;
}
void Update ()
{
// Move
player.transform.position +=
player.transform.right * speed * Time.deltaTime;
// Jump
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if(Input.GetKey(KeyCode.Space))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move (moveDirection * Time.deltaTime);
}
2 回答
这是旧的,但在谷歌搜索时发现了这一点 .
角色控制器不是为使用物理组件而设计的 . 拆下刚体和箱式对撞机 . 它使用模拟的胶囊对撞机进行检测 .
同时从代码中删除以下内容,输入应该再次正常工作 .
那段代码导致你无意中移动 .
您应该首先使用以下代码创建PlayerController类:
然后使用以下代码创建Player类:
您可以使用箭头键飞行并使用“a”键跳转 . 你可以简单地改变它 .