- Home /
Wall detection using Rays
I am making a 2.5D platformer controller and I am manipulation the player using transform.translate. I've been taking the script from this tutorial almost exactly, minus the animation portion and sliding: http://www.youtube.com/playlist?list=PLFt_AvWsXl0e-g21S-MiPArfEhlv9pUgC
I use a function that takes in the current speed, target speed and an acceleration to calculate how much to move the character in order to achieve acceleration and a bit of a sliding motion when changing directions.
I pass the amount to move the character into a variable
Then finally pass that variable into another function that detects floors and ceilings using rays to prevent the player from clipping through walls
(most of this is explained in the first two videos)
The plan was to modify the wall jump script he used in video # 6, but there was a problem when sliding down walls that did not have a ledge below it. The player would continue a slow decent despite there not being a wall anymore.
The first thing I change was to make the physics function make horizontal detections even if there is no horizontal inputs; but that lead to another issue that I can't pin down.
the player will now only wall slide if you hold the directional key toward the wall!!!!!!
I have checked the x transform to ensure that the player isn't moving unintentionally, but for some reason the wall detection just stops unless you hold the directional key toward the wall. The absolute worse part is that it only stops some of the time.
Is this a limitation of using Rays? Or is something else going on? Any help would be greatly appreciated
I think it will be easier to help you if you show your script
i think it's just a logic problem more than the limitation of Ray
The code to move horizontally:
private float IncrementTowards(float n, float target, float a)
{
if (n == target)
{
return n;
}
else
{
float dir = $$anonymous$$athf.Sign(target - n); // must n be increased or decreased to get closer to target
n += a * Time.deltaTime * dir;
return (dir == $$anonymous$$athf.Sign(target-n))? n: target; // if n has now passed target then return target, otherwise return n
}
}
targetSpeed = Input.GetAxisRaw("Horizontal") * speed;
currentSpeed = IncrementTowards(currentSpeed, targetSpeed,acceleration);
currentSpeed is then put into a Vector 2 and then passed into this function:
move(Vector 2 amountTo$$anonymous$$ove)
{
amountTo$$anonymous$$ove.x = deltaX;
for (int i = 0; i<3; i ++)
{
// creates the rays
float dir = $$anonymous$$athf.Sign(deltaX);
float x = p.x + c.x + s.x/2 * dir;
float y = p.y + c.y - s.y/2 + s.y/2 * i;
ray = new Ray(new Vector2(x,y), new Vector2(dir,0));
Debug.DrawRay(ray.origin,ray.direction);
if (Physics.Raycast(ray, out hit,$$anonymous$$athf.Abs(deltaX) + skin, collision$$anonymous$$ask))
{
// Get Distance between player and ground
float dst = Vector3.Distance (ray.origin, hit.point);
// Stop player's horizontal movement after co$$anonymous$$g within skin width of a collider
if (dst > skin)
{
deltaX = dst * dir - skin * dir;
}
else
{
deltaX = 0;
}
movementStopped = true;
break;
}
}
}
Answer by Nony42 · Jan 15, 2014 at 04:50 AM
I have found a fix! First I made horizontal detection possible even if there is not input. Then in the PlayerPhysics script where it checks for wall collisions, I created another if() statement.
if(Physics.Raycast(ray, out hit, 0.006f, collisionMask))
{
if(hit.collider.tag == "Wall Jump" && !grounded)
{
canWallHold = true;
}
}
This was necessary because if there was no horizontal input, the original ray distance check would not reach far enough to detect the wall which is why the distance I checked was just a little bit longer than the skin's distance.
Then in the PlayerController script I also changed all the checks for wall holding to one if() statement
if(playerPhysics.canWallHold == true)
{
wallHolding = true;
}
else
{
wallHolding = false;
}
Your answer
Follow this Question
Related Questions
Raycast error 1 Answer
Make an Enemy Shoot through Raycast 1 Answer
change object material on mouse click. 2 Answers
Negative Positions Breaks Raycasts 1 Answer
How to touch select 3D objects 2 Answers