- Home /
Send Raycast to ground, place player at Raycast hit?
I've searched through the questions and couldn't quite find what I was looking for.
Within a trigger, how do you shoot out a Raycast from an object to have it collide with the nearest surface directly below, and when it hits, it places the object at that Y value? Sort of like a ceiling that comes down and smashes the player, to be able to place them on the ground once that collision happens.
From the Unity Scripting Reference : http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
// Raycast up to 100 meters down
function Update () {
var hit : RaycastHit;
if (Physics.Raycast (transform.position, -Vector3.up, hit, 100.0)) {
var distanceToGround = hit.distance;
// location of hit -> hit.point
var pointOfRayHit = hit.point;
Debug.Log( "Ray : hit.distance = " + hit.distance + " : hit.point = " + hit.point );
}
}
Answer by TheDDestroyer12 · Jan 10, 2015 at 07:44 PM
The first step is to cast a raycast and get the location of the hit. This can be done like this (C# code):
/*
* Create the hit object
* This will later hold the data for the hit
* (location, collided collider etc.)
*/
RaycastHit hit;
/*
* The ray length.
* Modify it to change the length of the Ray.
*/
float distance = 100f;
/*
* A variable to store the location of the hit.
*/
Vector3 targetLocation;
/*
* Cast a raycast.
* If it hits something:
*/
if(Physics.Raycast(transform.position, Vector3.down, out hit, distance)) {
/*
* Get the location of the hit.
* This data can be modified and used to move your object.
*/
targetLocation = hit.point;
}
That is pretty simple stuff. The next step is to modify the location so that the object will be perfectly aligned with the ground. This can be done like this (C# code):
/*
* Add half of the height of the object to move to the Y coordinate of the location.
*/
targetLocation += new Vector3(0, transform.localScale.y / 2, 0);
/*
* Move the object to the calculated location.
*/
transform.position = targetLocation;
Here is a working example class (C# code):
public class TestClass : MonoBehaviour {
/*
* The wanted length for the Raycast.
*/
public float distance = 100f;
void Update() {
/*
* Create the hit object.
*/
RaycastHit hit;
/*
* Cast a Raycast.
* If it hits something:
*/
if(Physics.Raycast(transform.position, Vector3.down, out hit, distance)) {
/*
* Set the target location to the location of the hit.
*/
Vector3 targetLocation = hit.point;
/*
* Modify the target location so that the object is being perfectly aligned with the ground (if it's flat).
*/
targetLocation += new Vector3(0, transform.localScale.y / 2, 0);
/*
* Move the object to the target location.
*/
transform.position = targetLocation;
}
}
}
Hope this helps!
Your answer
Follow this Question
Related Questions
What's a good way to find the ground normal? 1 Answer
How do I use RayCast in a 2D Platformer game? 1 Answer
Select a group of gameobjects that are contiguous 1 Answer
Layered Collisions 1 Answer
Cylinder Ground Collision 0 Answers