レイの原点から衝突点までの距離
レイの場合、距離はレイの原点から衝突点までのベクトルの大きさを表します。
sweptVolume や sphereCast では、距離は、物体の原点から他のコライダーに接触する移動点までのベクトルの大きさを表します。
RaycastHit.point は衝突が発生する空間位置を表すことに注意してください。
using UnityEngine;
public class Example : MonoBehaviour { // 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. float moveForce = 1.0f;
// Torque for left/right rotation. float rotateTorque = 1.0f;
// Desired hovering height. float hoverHeight = 4.0f;
// The force applied per unit of distance below the desired height. float hoverForce = 5.0f;
// 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. float hoverDamp = 0.5f;
// Rigidbody component. Rigidbody rb;
void Start() { rb = GetComponent<Rigidbody>();
// Fairly high drag makes the object easier to control. rb.drag = 0.5f; rb.angularDrag = 0.5f; }
void 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);
RaycastHit hit; Ray downRay = new Ray(transform.position, -Vector3.up);
// Cast a ray straight downwards. if (Physics.Raycast(downRay, out hit)) { // The "error" in height is the difference between the desired height // and the height measured by the raycast distance. float 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. float upwardSpeed = rb.velocity.y; float lift = hoverError * hoverForce - upwardSpeed * hoverDamp; rb.AddForce(lift * Vector3.up); } } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.