Good day, i have an isue with my player movement script, I don’t know how to make the slope handler system with the Character Controller component(is the “_controller” variable) , i mean I disabled the gravity of my rigid body. There are the functions of movement, jump, slope detection and slope move direction:
void CharacterMovement()
{
valorDelta = Time.deltaTime; // This get the value of deltaTime
float horizontal = InputManager.MainJoystic().x;
float vertical = InputManager.MainJoystic().z;
//Change The Orientation of the player model
Vector3 inputDir = characterCore.forward * vertical + characterCore.right * horizontal;
if (inputDir != Vector3.zero)
{
_playerObj.forward = Vector3.Slerp(_playerObj.forward, inputDir.normalized, Time.deltaTime * RotationSpeed);
}
//////////////End of change Orientation/////////////////////////////
testHorizontal = horizontal; //Shows horizontal movement value
testVertical = vertical; //Shows front/back movement value
rotationY += InputManager.SecondJoystic().x * Time.deltaTime * RotationSpeed;
rotationX -= InputManager.SecondJoystic().z * Time.deltaTime * RotationSpeed;
rotationX = Mathf.Clamp(rotationX, -90f, 90f);
testRotationY = rotationY;
testRotationX = rotationX;
//Swimmig Condition for in-water camera rotation
if (_isSwiming == false)
{
_cameraY._isRotating = true;
transform.localRotation = Quaternion.Euler(0, rotationY, 0);
}
else if (_isSwiming == true)
{
_cameraY._isRotating = false;
transform.localRotation = Quaternion.Euler(rotationX, rotationY, 0);
}
//End of Swimming camera controller
Vector3 direction;
//Run Input Action
if(InputManager.XButton() && currentAmmo > 0 && _isGround == true)
{
if((vertical != 0 && horizontal != 0) || ((vertical > 0 || vertical < 0) || (horizontal > 0 || horizontal < 0)))
{
currentAmmo -= 2;
}
_uiManager.UpdateStamina(currentAmmo);
_speed = normalSpeed * 2;
direction = new Vector3(horizontal,0 , vertical);
}
else
{
_speed = normalSpeed;
direction = new Vector3(horizontal,0 , vertical);
_gravity = 1.3f;
}
//Wall running action
if (wallRunning)
{
_speed = normalSpeed * 2;
direction = new Vector3(0,0 , vertical);
_yVelocity = 0;
}
//////////////SLOPE MECHANIC/////////___________//////////////
//if(OnSlope() && !exitingSlope)
moveDirection = direction;
if(OnSlope())
{
//PhysicsA.AddForce(GetSlopeMoveDirection() * _speed * 20f, ForceMode.Force);
direction = new Vector3(GetSlopeVar.x, GetSlopeVar.y * -1f, GetSlopeVar.z) ;
//if (_yVelocity > 0)
// if (_yVelocity > 0)
// {
// _yVelocity = Vector3.down.magnitude;
_yVelocity = GetSlopeVar.y * -1f;
// }
}
//////////////eND sLOPE //////________________//////////
Vector3 velocity = direction * _speed;
velocity.y = _yVelocity;
velocity = transform.transform.TransformDirection(velocity);
globalVelocity = velocity;
_controller.Move(velocity * Time.deltaTime);
}
void JumpAction(bool grounded)
{
if (grounded == true)
{
_yVelocity = -1; //Stop the falling velocity
_canDoubleJump = false;
//Jump input
if (InputManager.AButton())
{
StartCoroutine(JumpFalling());
_yVelocity = _jumpHeight;
_canDoubleJump = true;
exitingSlope = true;
}
//Hi Jump for stage testing
if(InputManager.BButton() && currentAmmo > 0)
{
StartCoroutine(JumpFalling());
_yVelocity = 100;
exitingSlope = true;
}
}
else
{
//Second jump input
if (InputManager.AButton())
{
if (_canDoubleJump == true)
{
_yVelocity = _jumpHeight;
_canDoubleJump = false;
}
}
//Swiming gravity
if (_isSwiming == false)
{
_yVelocity -= _gravity * Time.deltaTime * 50;
}
else if (_isSwiming == true)
{
_yVelocity = 0;
}
}
}
//Decides if the character are on a slope depending of the angle
private bool OnSlope()
{
if(Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight * 0.5f + 0.3f ))
{
float angle = Vector3.Angle(Vector3.up, slopeHit.normal);
_slopeAngleShow = angle;
return angle < maxSlopeHandle && angle != 0;
}
return false;
}
//
private Vector3 GetSlopeMoveDirection()
{
//return Vector3.ProjectOnPlane(moveDirection, slopeHit.normal).normalized;
return Vector3.ProjectOnPlane(moveDirection, slopeHit.normal).normalized;
}