返回介绍

CharacterController.velocity 速度

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

JavaScript => public var velocity: Vector3;
C# => public Vector3 velocity;

Description 描述

The current relative velocity of the Character (see notes).

角色当前的相对速度。

This allows you to track how fast the character is actually walking for example when he is stuck at a wall this value will be the zero vector.

它允许你追踪角色实际走的有多快,例如,当他被卡在墙壁里,这个值将变为0向量。

Note: The velocity returned is simply the difference in distance for the current timestep before and after a call to CharacterController.Move or CharacterController.SimpleMove. The velocity is relative because it won't track movements to the transform that happen outside of the CharacterController (e.g. character parented under another moving Transform, such as a moving vehicle).

注释:返回速度在调用CharacterController.Move或者CharacterController.SimpleMove之前和之后的时刻是不同的。速度是相对的,因为它不会追踪发生在CharacterController之外的变换的运动(例如,角色继承于另一个运动着的变换之下,比如一个运动着的车辆)。

JavaScript:

	function Update () {
		var controller : CharacterController = GetComponent.<CharacterController>();
		var horizontalVelocity : Vector3 = controller.velocity;
		horizontalVelocity = Vector3(controller.velocity.x, 0, controller.velocity.z);
 
		// The speed on the x-z plane ignoring any speed 
		var horizontalSpeed : float = horizontalVelocity.magnitude;
		// The speed from gravity or jumping
		var verticalSpeed : float = controller.velocity.y;
		// The overall speed
		var overallSpeed : float = controller.velocity.magnitude;
	}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    void Update() {
        CharacterController controller = GetComponent<CharacterController>();
        Vector3 horizontalVelocity = controller.velocity;
        horizontalVelocity = new Vector3(controller.velocity.x, 0, controller.velocity.z);
        float horizontalSpeed = horizontalVelocity.magnitude;
        float verticalSpeed = controller.velocity.y;
        float overallSpeed = controller.velocity.magnitude;
    }
}

CharacterController

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

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

发布评论

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