- Home /
issue applying vector for slope sliding
trying to implement crouch sliding in my game down slopes above some threshhold (meaning that the player should speed up if they are facing around the direction of the slope going down.) I am also trying to implement sliding down slopes above 80 deg.
Having trouble adding the vector of the slope to the player velocity so that they actually slide down. My current code launches the player upwards when they hit a slope with any velocity, and I know that its because of how I'm applying the slope vector but I'm not sure how else to do it. Any help greatly appreciated, code below:
void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.gameObject.layer == 3)
{
//calculates the downslope vector of the surface at the point of contact
Debug.DrawLine(transform.position, hit.point, Color.red);
hitNormal = hit.normal;
hitTransform = hit.transform;
groundAngle = Vector3.Angle(Vector3.up, hitNormal);
Vector3 axis = Vector3.Cross(Vector3.up, hitNormal);
slopeVector = Quaternion.AngleAxis(90 + groundAngle, axis) * Vector3.up + transform.position;
Debug.DrawLine(transform.position, slopeVector, Color.green);
Debug.DrawLine(transform.position, velocity + slopeVector, Color.blue);
print("Slope Velocity Vector: " + (velocity + slopeVector));
}
}
void FixedUpdate()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, collidable);
print(isGrounded);
velocity.x = velocity.x / setSpeed;
velocity.z = velocity.z / setSpeed;
//add gravity and frictional/sliding forces to the vector then apply
if(isGrounded && !Input.GetButtonDown("Jump"))
{
if (hitTransform != null)
{
if(hitTransform.position.y <= transform.position.y)
{
if(isCrouching && Vector3.Angle(velocity, slopeVector) < 70)
{
velocity = slopeVector;
}
else if (groundAngle >= 50)
{
velocity = slopeVector;
}
}
}
}
else
{
velocity.y += gravity * Time.deltaTime;
}
controller.Move(velocity * Time.deltaTime);
}
Thank you for taking the time to read through my question, I hope that I have made it clear.
I forgot to mention, for slope sliding, I tried to just multiply the slope vector by some scalar, to make it larger than the velocity of the character moving, but it did not work well. I think I just have some gaps in my understanding of vectors, but I'm still struggling even after reading over the documentation. I understand that vectors are position values with a direction cast from the origin and I guess where I'm struggling is how to apply a vector to my player which is centred in the player's transform, still incorporates movement input from the player, and results in a force that pushes them down the slope. Maybe I can figure it out if I just keep trying to brute force it.
Your answer
Follow this Question
Related Questions
Why does my character fly upwards when I click play? 1 Answer
How to hold objects in third person? 1 Answer
Using rigidbody for collisions only, not movements? 1 Answer
My character can stop movement of the Death Star! 2 Answers
CharacterController.Move() is causing unwanted vertical movement . . . how to fix? 2 Answers