- Home /
The Distance in Raycast?
Hi guys, I was following a tutorial online and I'm getting a little confused with the Distance in the Raycast. So pretty much what is going on is that DeltaY which is also being used as the gravity (-20) is being used as the distance for the raycast Mathf.Abs (deltaY)
So if, we end up actually Checking.. the distance, then it seems like the distance 20 is omitted or something. So can someone kind of give an explanation on that. Is the number 20 just being ignored?
[RequireComponent(typeof(BoxCollider))]
public class PlayerPhysics : MonoBehaviour
{
public LayerMask collisionMask;
private BoxCollider collider;
private Vector3 size;
private Vector3 center;
private float boarder = .005f;
[HideInInspector]
public bool grounded;
Ray ray;
RaycastHit hit;
void Start ()
{
collider = GetComponent<BoxCollider>();
size = collider.size;
center = collider.center;
}
public void Move(Vector2 moveAmount)
{
float deltaY = moveAmount.y;
float deltaX = moveAmount.x;
Vector2 position = transform.position;
grounded = true;
for (int i = 0; i<3; i++)
{
float dir = Mathf.Sign (deltaY);
float x = (position.x + center.x - size.x/2) + size.x/2 * i;
float y = position.y + center.y + size.y/2 * dir;
ray = new Ray(new Vector2(x,y), new Vector2(0,dir));
Debug.DrawRay(ray.origin,ray.direction);
if (Physics.Raycast(ray, out hit, Mathf.Abs (deltaY), collisionMask))
{
//Get Distance between player and ground
float Dist = Vector3.Distance (ray.origin, hit.point);
if(Dist > boarder)
{
deltaY = Dist * dir + boarder; /
}
else
{
deltaY = 0;
}
grounded = false;
break;
}
}
Vector2 finalTransform = new Vector2(deltaX,deltaY);
transform.Translate (finalTransform);
}
}
Answer by rutter · Jun 04, 2014 at 07:27 PM
The distance
argument sets the maximum distance your ray will travel during the cast.
For example, if you set a distance of 20
, your ray specifies an origin and direction, and the cast will continue up to 20 units (or until it hits something).
One possibility: it looks like your ray is coming from the center of the player (or whatever object you're moving). You might instead want it to come from the outermost edge of that object.
Suppose for a moment that your player has a radius of 5 units, and moves down 1 unit. You cast a ray from the center of the player, down 1 unit... surprise, surprise, it doesn't hit anything, because that's still inside the player.
There are many ways you could adjust for height, but the simplest is probably something like this:
float playerRadius = 5f; //wild guess, use something smart
y += playerRadius * dir;
You could also check the player's collider bounds. Some colliders, such as Box2D or capsule colliders, offer specific height information.
Oh so its the max distance of the Raycast. This was actually a box, and it was set up so that a for loop, would set the x position on the left middle and center of the box, and then actually be brought to the bottom with the y position so that when the ray was actually cast, a ray would be co$$anonymous$$g from 3 different points from the bottom of the box.
Answer by robertbu · Jun 04, 2014 at 07:28 PM
I don't see where anything you have here directly relates to gravity. I'm guessing that moveAmount is related to gravity, but it likely is also multiplied by Time.deltaTime. But none of that matters. This is the line to take a look at:
float dir = Mathf.Sign (deltaY);
Mathf.Sign() returns either -1 or 1. So your Raycast() will be in the direction you are moving and have a distance of 1.0.
yea threw me off for a while.
if(Dist > boarder)
{
deltaY = Dist * dir + boarder; /
}
this line right here is about the only thing I am kind of struggling with to understand. But I do get the gist of it.
Your answer
Follow this Question
Related Questions
Problems with Physics.Boxcast ( reward for fixing 10€ via paypal ) 1 Answer
iam using raycast.hit function.am trying to move vehicle over the terrain ups and down?? 0 Answers
Raycast distance affected by momentum of character 0 Answers
Tracking distance after Instantiate 2 Answers
Making a 2d "radar" for space game 2 Answers