Right now I'm finalizing my character raycast controller, but have come to the realization that Unity's tag system, which I'm using as a way for my raycasts to detect different types of platforms, is limited and may cause issues further into development when I start adding more platform types, or use the controller for AI (do I want my AI to be affected the same way the player is affected by platforms?).
So far I've got a bunch of different platforms, like passable platforms, icy blocks and jump boosts. These all work great, and change the character's default movement parameters, but I rely on things like this in order for changes to be made:
if (hit.collider.tag == "platformPassable")
Is there a better way to do this? I am not the most experienced programmer, but my initial solution was to create a Platform class with a TypeOfPlatform enum that will allow me to make sub classes that implement changes based on this type, but I'm worried that having to access a component for each raycast on a platform on which the character or AI is standing will lead to horrible performance issues. Would I have to reference the base class in the character controller, or would I access the character controller in the base class?
I'm just not very comfortable with inheritances like this and would appreciate some guidance.
Update: here's a segment of my code (there are many other likes this) that hopefully helps illustrate what I believe will become problematic as I continue development:
private void MoveVertically(ref Vector3 deltaMovement)
{
float directionY = Mathf.Sign(deltaMovement.y);
float rayLength = Mathf.Abs(deltaMovement.y) + skinWidth;
for (int i = 0; i < verticalRayCount; i++)
{
Vector2 rayOrigin = (directionY == -1) ? raycastOrigins.bottomLeft : raycastOrigins.topLeft;
rayOrigin += Vector2.right * (verticalRaySpacing * i + deltaMovement.x);
RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, collisionMask);
Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength, Color.red);
if (hit)
{
if (hit.collider.tag == "platformPassable" || hit.collider.tag == "icePassable")
{
State.IsCollidingWithPassablePlatform = true;
if (directionY == 1)
{
State.IsCollidingAbove = false;
continue;
}
if (CanJumpDown)
{
State.IsCollidingBelow = false;
continue;
}
}
if (hit.collider.tag == "iceNormal" || hit.collider.tag == "icePassable")
{
Parameters.accelerationTimeGrounded = 1.0f;
Parameters.accelerationTimeAirborne = 3.0f;
}
else if (overrideParameters == null)
{
Parameters.accelerationTimeGrounded = defaultParameters.accelerationTimeGrounded;
Parameters.accelerationTimeAirborne = defaultParameters.accelerationTimeAirborne;
}
deltaMovement.y = (hit.distance - skinWidth) * directionY;
rayLength = hit.distance;
if (State.IsClimbingSlope)
{
deltaMovement.x = deltaMovement.y / Mathf.Tan(State.SlopeAngle * Mathf.Deg2Rad) * Mathf.Sign(deltaMovement.x);
}
State.IsCollidingBelow = directionY == -1;
State.IsCollidingAbove = directionY == 1;
CanJumpDown = false;
}
}
As you can see there are a lot of platform types that are somewhat similar. Plus, since this script will be placed on any character, such as the player or the enemy, both of these entities will be bound to the same movement parameters. What if I didn't want an enemy to slide around on an ice block? I'm not sure there's a way to prevent this in my code in its current structure.
collider.CompareTag(“platformPassable”)
instead ofcollider.tag == “platformPassable”
because it is faster than string comparison. \$\endgroup\$