返回介绍

RaycastHit.distance 距离

发布于 2019-12-18 15:38:23 字数 4270 浏览 1362 评论 0 收藏 0

JavaScript => public var distance: float;
C# => public float distance;

Description 描述

The distance from the ray's origin to the impact point.

从射线的原点到触碰点的距离。

JavaScript:

	// Movable, levitating object.
 
	// This works by measuring the distance to ground with a
	// raycast then applying a force that decreases as the object
	// reaches the desired levitation height.
 
	// Vary the parameters below to
	// get different control effects. For example, reducing the
	// hover damping will tend to make the object bounce if it
	// passes over an object underneath.
 
	// Forward movement force.
	var moveForce = 1.0;
 
	// Torque for left/right rotation.
	var rotateTorque = 1.0;
 
	// Desired hovering height.
	var hoverHeight = 4.0;
 
	// The force applied per unit of distance below the desired height.
	var hoverForce = 5.0;
 
	// The amount that the lifting force is reduced per unit of upward speed.
	// This damping tends to stop the object from bouncing after passing over
	// something.
	var hoverDamp = 0.5;
 
	// Rigidbody component.
	var rb: Rigidbody;
 
	function Start () {
		rb = GetComponent.<Rigidbody>();
 
		// Fairly high drag makes the object easier to control.
		rb.drag = 0.5;
		rb.angularDrag = 0.5;
	}
 
 
	function FixedUpdate () {
		// Push/turn the object based on arrow key input.
		rb.AddForce(Input.GetAxis("Vertical") * moveForce * transform.forward);
		rb.AddTorque(Input.GetAxis("Horizontal") * rotateTorque * Vector3.up);
 
		var hit: RaycastHit;
		var downRay = new Ray(transform.position, -Vector3.up);
 
		// Cast a ray straight downwards.
		if (Physics.Raycast(downRay, hit)) {
			// The "error" in height is the difference between the desired height
			// and the height measured by the raycast distance.
			var hoverError = hoverHeight - hit.distance;
 
			// Only apply a lifting force if the object is too low (ie, let
			// gravity pull it downward if it is too high).
			if (hoverError > 0) {
				// Subtract the damping from the lifting force and apply it to
				// the rigidbody. 
				var upwardSpeed = rb.velocity.y;
				var lift = hoverError * hoverForce - upwardSpeed * hoverDamp;
				rb.AddForce(lift * Vector3.up);
			}
		}
	}

C#:

using UnityEngine;
using System.Collections;
 
public class ExampleClass : MonoBehaviour {
    public float moveForce = 1.0F;
    public float rotateTorque = 1.0F;
    public float hoverHeight = 4.0F;
    public float hoverForce = 5.0F;
    public float hoverDamp = 0.5F;
    public Rigidbody rb;
    void Start() {
        rb = GetComponent<Rigidbody>();
        rb.drag = 0.5F;
        rb.angularDrag = 0.5F;
    }
    void FixedUpdate() {
        rb.AddForce(Input.GetAxis("Vertical") * moveForce * transform.forward);
        rb.AddTorque(Input.GetAxis("Horizontal") * rotateTorque * Vector3.up);
        RaycastHit hit;
        Ray downRay = new Ray(transform.position, -Vector3.up);
        if (Physics.Raycast(downRay, out hit)) {
            float hoverError = hoverHeight - hit.distance;
            if (hoverError > 0) {
                float upwardSpeed = rb.velocity.y;
                float lift = hoverError * hoverForce - upwardSpeed * hoverDamp;
                rb.AddForce(lift * Vector3.up);
            }
        }
    }
}

See Also: Physics.Raycast, Physics.Linecast, Physics.RaycastAll.

RaycastHit

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

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

发布评论

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