I’m trying to move the player following a direction Vector, so it moves up and down the slope nicely. My approach works in the uphill direction, but not in the downhill direction. Downhill the Vector’s y component has the wrong sign and I can’t figure out a solution.
MyCode:
//getting inputs
inputDirection = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
//getting ground info
RaycastHit hitInfo;
if(Physics.SphereCast(transform.position + Vector3.up * groundCheckOffset, groundCheckRadius, -Vector3.up, out hitInfo, groundCheckDistance, groundLayer)){
grounded = true;
groundAngle = Vector3.SignedAngle(new Vector3(inputDirection.x, 0, inputDirection.y), hitInfo.normal, Vector3.Cross(new Vector3(inputDirection.x, 0, inputDirection.y), hitInfo.normal)) - 90f;
}
else{
grounded = false;
groundAngle = 0;
}
//calculating the velocity vector
velocity = new Vector3(inputDirection.x, Mathf.Sin(groundAngle * Mathf.Deg2Rad), inputDirection.y).normalized * targetSpeed;
I know this probably isn’t a very good approach, but this is what I came up with.
EDIT:
added a (bad) illustration showing my problem: green sphere is the spherecast, red vector is the velocity. When going downhill it should be the dashed vector.
