- Home /
unity2d issue with linecasts.
Hello I am having a problem where the linecast will only detect a collision to a floor in the middle of the object but in my game there are mutliple floating objects and to get the optimin jump you need to jump at the end of the block. This is described in the picture below.
my code for this is:
private Transform groundDetector;
void Awake()
{
groundDetector = transform.Find("GroundCheck");
}
void Update()
{
grounded = Physics2D.Linecast(transform.position, groundDetector.position, 1 << LayerMask.NameToLayer("Terrain"));
if (grounded) {
doubleJump = false;
}
if ((grounded || !doubleJump) && jump) {
// Add a vertical force to the player.
anim.SetBool("Ground", false);
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0);
rigidbody2D.AddForce(new Vector2(0f, jumpForce));
if(!grounded && !doubleJump){
doubleJump = true;
}
}
}
for some reason the jump actually works but after the mid-point thedouble jump does not work
Answer by Razouille · Mar 20, 2014 at 04:11 PM
A simple way is to create 3 small boxes which will all fire a Linecast : one on the middle, one on the left and one on the right of the player, then you'll have to check if all hits are null in order to avoid your problem.
A second solution is to use the OverlapArea
With your current script a third solution will be to create 3 ground detectors and do a LineCast to each of them. You can find an example here
sorry I found the link but it doesn't show much information. So if you could expand on how to use it.
and one thing on the line cast on the left and right how would you get this script to detect the collision so I could initiate the jump.
I added a third solution to my post, it's better than use an OverlapArea