Question by
ToxicBlotch1 · Jun 10, 2017 at 11:47 PM ·
collisionmovementraycast
Ray casting collision detection, not working for moving right.
I am try to build my first plat former game. I am using 3D mode to allow me to hopefully create my vision.
I have this code that I am using to detect if I can move left or right.
//Checkcollisions Left And Right for (var i = 0; i < 3; i++) { var dir = Mathf.Sign(deltaX); var y = playerpos.y + center.y - size.y / 2 + size.y / 2 * i; if (dir == -1) { var x = (playerpos.x - center.x - size.x / 2); ray = new Ray(new Vector2(x, y), new Vector2(dir, 0)); } else { var x = (playerpos.x + center.x + size.x / 2); ray = new Ray(new Vector2(x, y), new Vector2(dir, 0)); } Debug.DrawRay(ray.origin, ray.direction); if (Physics.Raycast(ray, out rayHit, Mathf.Abs(deltaX) + skin, collisionMask)) { Debug.Log("raycast hit!"); float dst = Vector3.Distance(ray.origin, rayHit.point); if (dst > skin) { deltaX = dst * dir + skin; Debug.Log("one"); } else { Debug.Log("two"); deltaX = 0; } break; } }
This code is working just fine for moving left. and when I hit a wall I stop as expected. But If I moving right, I pass through the object. As you can see form the code I have debug rendering the ray lines, and I am able to see the ray's in the correct place in relation to my player object.
I am literal now at the end of my rope and any help or advise that anyone can give me to help resolve this problem will be amazing.
Thanking you all in advance.
Comment