Hello, I’ve been developing code for a 3D isometric game and I’m currently having trouble with collisions. I’m adding a roll/dodge/dash move that is supposed to move the player forward while disabling all other movement. The problem I’m running into is trying to get the dash to stop if it hits a wall. Currently I have player movement set up like this:
void Move()
{
//if we're dodging no movement
if (dodge) return;
//calculate forward rotation based on input and incline and assign to point variable
CalculateForward();
//if slope ahead compared to the current location is too high, return.
if (groundAngle >= maxGroundAngle) return;
//check if we're hitting anything on the ground layer in front of us.
if (Physics.Raycast(transform.position, point, out hitInfo, height + heightPadding, ground)) return;
//move the player the direction they are facing in order to account for y-axis changes in terrain
transform.position += point * moveSpeed * Time.deltaTime;
}
This all works as intended. When I move the character around it runs into walls and everything works like normal. When I try to do a dash, the character moves up and down inclines as they should, but the character moves through the walls. I tried solving it like this:
First, the Dodge function is called when the dodge button is pressed
void Dodge(Vector2 m)
{
//if we're not already dodging and we're on the ground
if (!dodge && grounded)
{
dodge = true;
if (m != Vector2.zero)
{
//snap player rotation to inputted direction
transform.forward = head;
}
else
{
}
//Start movement
StartCoroutine(DodgeMovement(0.5f));
}
}
Which calls the following coroutine:
IEnumerator DodgeMovement(float duration)
{
//reset timer
float time = 0f;
while (time < duration)
{
//movement
dodge = true;
//Increase the timer
time += Time.deltaTime;
yield return null;
}
//finish movement and remove dodge status.
dodge = false;
}
The next function is called on FixedUpdate, but here’s the problem.
void DodgeMove()
{
if (dodge && !Physics.Raycast(transform.position, point, out hitInfo, height + heightPadding, ground))
{
//wall collision
print("dodging");
transform.position += point * dashSpeed * Time.deltaTime;
}
else
{
moveSpeed = baseMoveSpeed;
}
}
When I include the collision check in the if statement, the dodge has no movement but still prints “dodging” to the console. When I remove the collision check it works as intended but the player moves through walls, so same problem as before. When I try putting the collision check before both functions then neither the normal movement nor the dash movement works.
Anyone have any idea why this check disables the movement? I can provide more information or files if necessary. Thank you in advance!