Version: 2017.2
public CollisionFlags Move (Vector3 motion);

描述

采用绝对移动增量的更复杂移动函数。

尝试通过 motion 移动该控制器,此运动将仅受碰撞约束。 它将沿着碰撞体滑动。 CollisionFlags 总结了此 Move 期间发生的碰撞。 该函数未施加任何重力。

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { public float speed = 6.0F; public float jumpSpeed = 8.0F; public float gravity = 20.0F; private Vector3 moveDirection = Vector3.zero; void Update() { 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.GetButton("Jump")) moveDirection.y = jumpSpeed; } moveDirection.y -= gravity * Time.deltaTime; controller.Move(moveDirection * Time.deltaTime); } }