Hi, I’m having trouble understanding the basic concept of some code for this script. On a youtube tutorial, the tutorial maker basically says that this variable skin is being used for some “space” between the ground and the player’s collider. and that if the distance is greater than the skin (.005) , then take deltaY, (max distance) and have it assigned like this deltaY = distance * dir + .005. If that is not the case then have deltaY = 0. So that’s all well and good, but i’m having trouble understanding what that has to do with the player being on the ground. So I can somewhat understand what everything is, but i’m having a hard time actually connecting the dots.
if(Dist > boarder)
{
deltaY = Dist * dir + boarder;
}
else
{
deltaY = 0;
}
[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);
}
}
Here is the actual link to the tutorial
The part I am talking about starts at 20:30 on the video