Hello,
as part of a custom entity controller for my specific use case i need to make the moving entity move along angled walls. so far i have 90% of it done and working. My approach is as follows:
Basic move function
public void Move(Vector3 direction)
{
transform.Translate(CanMove(direction) * _speedMultipler * Time.deltaTime);
}
CanMove check
private Vector3 CanMove(Vector3 direction)
{
//boxcast in movedirection
if (Physics.BoxCast(_bc.transform.position,
new Vector3(((_bc.size.x * 0.95f) / 2), (_bc.size.y / 2), ((_bc.size.z * 0.95f) / 2)),
direction,
out RaycastHit hit,
_bc.transform.rotation,
_castDistance
))
{
var castHeight = hit.point.y;
//(debug) draw impact ray
if (_drawCheckRays) Debug.DrawRay(hit.point, -MaximizeVector(direction, 1f), _rayColor, 0f);
//check if floor slope or wall
if (!SlopeCheck(hit, castHeight, direction))
{
//check wall angle
return WallAngle(hit, castHeight, direction);
}
}
return direction.normalized;
}
SlopeCheck() simply checks whether the object hit is a walkable slope or a wall and returns true if the slope is walkable, false if the slope is steeper than _param degrees.
WallAngle
private Vector3 WallAngle(RaycastHit hit, float castHeight, Vector3 direction)
{
//store angle of the wall
var hitAngle = Vector3.Angle(hit.normal, -direction);
Debug.Log($"Wall Angle: {hitAngle}");
//check if wall angle is movable along, also excluding 0-angles because that's causing bugs
if (hitAngle <= _maxDiagonalWall && hitAngle > 0.01)
{
//rotate movement vector by wall angle
return ((Quaternion.AngleAxis(hitAngle, Vector3.up) * direction).normalized * 0.5f); //TODO: calculate slowness factor through angle
}
return new Vector3();
}
Here the magic happens and it is also where the issue lies. I’m getting the wall angles (which are perfectly fine) and then check if the angled wall should be able to be walked along. All good here, too. However, during the rotation of the vector according to the wall angle the issue arises.
If in the rotation function hitAngle is positive, my character can move along the angled wall horizontally perfectly fine how it should be, but vertically my character gets stuck.
If in the rotation function hitAngle is negative, my character can move along the angled wall vertically perfectly fin, but horizontally gets stuck.
I somehow have to determine the ±-value of hitAngle dynamically, but don’t know how. I know that the hit.normal kind of stores the direction but calculating it from that would probably mess up at 45° angles.