#Raycasting Distance Problem
1 messages · Page 1 of 1 (latest)
yes
I am making a game where my character teleports on the point I click. I am trying to prevent player to go into blocks, so I used raycast2d to detect if there are any blocks.
But there is a problem: If I click on just top of an object, because characters center point spawns, bottom part of character intercepts with block first then comes up. To prevent this I thought something like that.
x = Player's height
y = distance between clicked position and nearest object
1- Send rays to 4 side
2- If it collides an object with Ground tag, calculate distance(y)
3- If distance(y) < x/2 (half of the height)
4- player.transform.position = clicked position + (x/2 - y)
so I created a code for it and it is this:
{
//If character is on ground and left mouse button is clicked, check how far the point you clicked from the player and teleport
if(Input.GetMouseButtonDown(0) && IsGrounded())
{
//Calculating distance between mouse clicked point and player point
Vector2 playerPosition = player.transform.position;
Vector2 clickedPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 distanceFromPlayer = clickedPosition - playerPosition;
//Checking if mouse clicked point is a game object that can't be teleported
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity);
//Getting players center height
playerCollider = GetComponent<BoxCollider2D>();
playerTop = playerCollider.bounds.max;
playerBottom = playerCollider.bounds.min;
playerHeight = playerTop.y - playerBottom.y;
Debug.Log(playerHeight);
Debug.Log(distanceFromBlockBottom);
if (hit.collider == null)
{
if (distanceFromPlayer.magnitude < 3)
{
CalculateBlockDistance();
if(distanceFromBlockBottom < playerHeight / 2)
{
Vector2 newHeight;
newHeight.y = playerHeight/2 - distanceFromBlockBottom;
newHeight.x = 0;
player.transform.position = newHeight + clickedPosition;
}
else
{
player.transform.position = clickedPosition;
}
}
}
}
}
public void CalculateBlockDistance()
{
//Getting players center height
playerCollider = GetComponent<BoxCollider2D>();
playerTop = playerCollider.bounds.max;
playerBottom = playerCollider.bounds.min;
playerHeight = playerTop.y - playerBottom.y;
Vector2 clickedPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit2D bottomRay = Physics2D.Raycast(ray.origin, -Vector2.up, 0.3f, groundLayer);
if (bottomRay.collider != null && bottomRay.collider.CompareTag("Ground"))
{
distanceFromBlockBottom = clickedPosition.y - bottomRay.distance;
}
}
but with this code this happens:
Wherever I click it teleports player to a upper point. I think calculating distance stuff wont work
I tried to take a screen video but my computer is basically a junk so it wasn't a huge success
please help me
Maybe https://docs.unity3d.com/ScriptReference/Physics2D.BoxCast.html would work. It casts a square isntead of a ray. So if you made it the size of your character you probably wouldn't need as much math.
can you please help me with code
how will I make it work
I mean how will I know how much up I need to teleport
i believe ray.origin is camera position
I use same code in teleporting and it works actually
let me change it tho
what should it be
Boxcast or Circle cast might help you here, since they are like raycasts but with thickness.
if you camera is orthongonal projection, then try to use the clickedPosXXX as origin in 2d.raycast
I didn't get how will I do this
how will I calculate distance with them
just look at the main camera (component) in scene to see if it is orthogonal