Hey everyone, I’m having an issue where my character will stick into the side of a wall instead of sliding down, or falling to the ground like they should. I am using a rigidbody and capsule collider combination. I am using a C#Messenger System to listen for inputs from my controls class. the movements function is called during fixedupdate.
I am using bitshift operators to store my inputs because i’m recording the characters inputs to be played back.
Here is a sample of my movement code and my OnCollisionStay
//PLAYER MOVEMENT
private void movement()
{
//Used for storing the characters movement Commands
Vector3 targetVelocity = Vector3.zero;
//Check for a New Input
if (activeCharacter)
{
//If The oldCommand does not equal the Current command then create/store an input change
if (oldCommand != command)
{
//record the current elapsed time
float elapsedTime = worldScript.elapsedTime;
//create the Input Record and Assign the oldCommand to Current command
recordingScript.createRecord(name, elapsedTime, command, transform.position.x);
oldCommand = command;
}
}
//MOVING FORWARD AND BACKWARD
if ((command forward) == forward)
{
faceLeft = false;
targetVelocity.x = 1;
if (!playerJumping)
{
animScript.walkingRight();
}
}
if ((command backward) == backward)
{
faceLeft = true;
targetVelocity.x = -1;
if (!playerJumping)
{
animScript.walkingLeft();
}
}
//JUMPING
if (((command jumping) == jumping))
{
//Make Sure I am on the Ground
if (!falling)
{
Debug.Log("STARTING JUMP");
canJump = false;
playerJumping = true;
animScript.jumping();
}
}
//Multiple my movements by MoveSpeed
targetVelocity *= moveSpeed;
//Modify the Characters Velocity and Determine the Change in Velocity
Vector3 velocity = rigidbody.velocity;
Vector3 velocityChange = (targetVelocity - velocity);
//Clamp the Velocity change to the MaxVelocity variable
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVel, maxVel);
velocityChange.y = 0;
//Add the Change in Velocity to the Current Character
rigidbody.AddForce(velocityChange, ForceMode.VelocityChange); //////////////////ORIGINAL
//NEEDS TO BE CHECKED FOR JUMP SPAMMING
//Player is Jumping
if (playerJumping)
{
Debug.Log("Jumping");
//Add The JumpForce to the Characters Y position
rigidbody.AddForce(new Vector3(0, jumpForce, 0), ForceMode.VelocityChange); //////////////////ORIGINAL
//Successively diminish the strength of the Jump
jumpForce -= jumpSpeed;
//Once the jumpForce is less than 0 the character is no longer ascending
if (jumpForce < 0)
{
falling = true;
}
//Limit the fall speed of the character
if (jumpForce < fallCap)
{
jumpForce = fallCap;
}
}
//Manually Apply Gravity to Character
rigidbody.AddForce(new Vector3(0, -gravity, 0));
//Always Assume Falling
falling = true;
}
//Check for CollisionStay - Check For Still on Ground
public void OnCollisionStay()
{
Debug.Log("OnCollisionStay");
//Issue with WallJumps Currently//
//Once the player is not Jumping - Reset JumpForce
if (!playerJumping)
{
Debug.Log("RESETTING JUMP");
jumpForce = resetJumpForce;
superJumpForce = superResetJumpForce;
canJump = true;
}
falling = false;
playerJumping = false;
}
So there are two problems i’m facing right now,
- My character sticks into the sides of walls when applying force into that wall. So i cling to walls…
- OnCollisionStay doesn’t check to see if im on the ground, just checks for continuous collision with an object. So I can super jump…
I solved these problems by using a CharacterController but in my game my character is required to interact with other objects that can push my character, whether he’s moving or not…
So i’m stuck between figuring out a solution to the rigidbody problem, or finding a solution to the character controller solution…
Please Help