Locking player's Y position while jumping and near a wall
I am trying to create a script that enables the player to attach themselves to walls that they jump at and run along them. The issue that I have is I can't seem to lock the players position so that they "attach" to the wall. I am also having issues finding out if the player is moving using rigidbody.velocity as its value doesn't seem to change at all during run time.
I have tried using rigidbody constraints but for some reason it will not freeze any constraints through code, or even manual changes during run time. Another method I attempted was to get the transform.position.y and change it directly by stating its value. This does not work either, and will not work to change the Y value. Below is the section of code from my script that handles what I am talking about:
void Start()
{
playerBody = GetComponent<Rigidbody>();
wallPos = transform.position;
}
void Update()
{
height = transform.position.y;
// check if player is jumping
if (height > 1f)
isJumping = true;
else
isJumping = false;
// check if player is moving
if (playerBody.velocity.x > 0f || playerBody.velocity.z > 0f)
isRunning = true;
else
isRunning = false;
// if player is running and jumping and space is pressed, check for walls to either side
if (isJumping && Input.GetKeyDown(KeyCode.Space))
{
CheckWallPosition();
// if there is a wall, freeze players position
if (wallOnRightSide || wallOnLeftSide)
{
playerBody.constraints = RigidbodyConstraints.FreezePositionY;
isWallRunning = true;
print(Time.time);
// if the player is wall running and space is pressed, release them from the wall
if (isWallRunning && Input.GetKeyDown(KeyCode.Space))
{
playerBody.constraints = RigidbodyConstraints.None;
print(Time.time);
}
}
}
// test to see if transform.position.y can be changed manually (does not work)
if (Input.GetKey(KeyCode.L))
height = 9;
}