返回介绍

CharacterController.Move 移动

发布于 2019-12-18 15:37:30 字数 3229 浏览 1256 评论 0 收藏 0

JavaScript => public function Move(motion: Vector3): CollisionFlags;
C# => public CollisionFlags Move(Vector3 motion);

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.

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

JavaScript:

// 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);
}

C#:

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);
    }
}

CharacterController

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文