I have walls that I can climb and I use triggers on them to be able to initiate climbing. I have two objects, one being the wall object which has a collider, and then another object as a child of the wall object which has my trigger that I’ve resized to a very thin, vertical line and position it slightly in front of the wall.
My player is only supposed to be able to climb if they are completely centered on that trigger collider but if my player is all the way to the left or right of the wall and still even slightly touching the trigger, they are allowed to climb which shouldn’t be possible.
The brown object is the wall and the green line is the trigger, and I’ve tried to combine Vector3.Dot with Vector3.Distance to make sure that my player is both touching the trigger/wall and is facing the trigger collider. I’ve used the wall and trigger collider positions for the Vector3.Dot and Vector3.Distance but the Dot products are roughly the same, and they differ significantly at every point on that trigger. Like the bottom of the trigger will give 0.500, the middle will give 0.900, and then the very top will give something like 0.480. Then the left side of the green line/trigger will range from 0.450 - 0.800, and the same goes with the right side, so I can’t use my return DotValue < 0.50f or > 0.45f since it won’t work consistently.
All I’m trying to do is make sure that my player is touching or at least extremely close to the trigger collider and is both facing it/looking at it and standing in the center of it, at any point on that green line, whether at the top, middle (which means being airborne/jumping) or the bottom on the ground. I’m currently trying a Raycast + Vector3.Distance instead of Vector3.Dot + Vector3.Distance but I’m currently unsure if there’s a better solution than that.
I solved the issue with Vector3.Dot I think, from my testing, and I used code almost identical to the one below. It took me a while to get to this point, but something like this?
private bool IsPlayerCloseToLadder()
{
Vector3 middlePoint = new Vector3(wallTrigger.position.x, transform.position.y, wallTrigger.position.z); // middle point of trigger collider
float distance = Vector3.Distance(new Vector3(transform.position.x, 0, transform.position.z), new Vector3(middlePoint.x, 0, middlePoint.z));
return distance >= 0.618f && distance <= 0.635f;
}
This seems to work, the value does not change at all based on my players vertical position. The only problem is that sometimes the value is different but I don’t know how to replicate the problem consistently yet, even with debugging.
Like if I enter the trigger and am coming into contact with the trigger collider which is the wallTrigger object, the distance value for example might be between 0.618f - 0.635f just like in my code and it’s very consistent which is good. But sometimes in that exact same position with my player facing the very same direction, the value will be for example 0.580f - 0.590f and I have no idea why.
Thanks for the reply. I’ve been looking into that as well, but I’m trying to make sure that before climbing begins, my player is both facing the center of the thin wall trigger collider and is also basically touching the wall/very close to it. I’ve already handled the moveTowards + rotation part, I’m just having trouble with checking the distance since it’s so inconsistent.