CharacterController.Move 移动

function Move (motion : Vector3) : CollisionFlags

Description描述

A more complex move function taking absolute movement deltas.

一个更加复杂的运动函数,每次都绝对运动。

Attempts to move the controller by motion, the motion will only be constrained by collisions. It will slide along colliders. CollisionFlags is the summary of collisions that occurred during the Move. This function does not apply any gravity.

尝试着通过动力移动控制器,动力只受限制于碰撞。它将沿着碰撞器滑动。CollisionFlags 是发生于Move的碰撞的概要。这个函数不应用任何重力。

using UnityEngine;
using System.Collections;

public class example : 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);
	}
}
// This script moves the character controller forward 
// and sideways based on the arrow keys.
//这个脚本用箭头键向前移动和侧移角色控制器。
// It also jumps when pressing space.
//当按下空格键时,它跳起。
// Make sure to attach a character controller to the same game object.
//确保把一个character controller组件附加到同一个游戏物体上。
//It is recommended that you make only one call to Move or SimpleMove per frame.
//建议你每帧只调用一次Move或者SimpleMove。 

var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;

private var moveDirection : Vector3 = Vector3.zero;

function Update() {
   var controller : CharacterController = GetComponent(CharacterController);
   if (controller.isGrounded) {
	 // We are grounded, so recalculate
	 // move direction directly from axes
	 //我们着地了,所以直接通过轴重新计算move direction。
	 moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,Input.GetAxis("Vertical"));
	 moveDirection = transform.TransformDirection(moveDirection);
	 moveDirection *= speed;     

     if (Input.GetButton ("Jump")) {
			moveDirection.y = jumpSpeed;
		}
   }
   // Apply gravity
   //应用重力。
   moveDirection.y -= gravity * Time.deltaTime; 
   // Move the controller
   //移动控制器。
   controller.Move(moveDirection * Time.deltaTime);
}
最后修改:2010年12月14日 Tuesday 15:09

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。