I’m trying to align my charatcter to the ground its standing on. To get the normal I do a boxcastall and then get the average between the normals of all surfaces hit. The problem is that at specific angles when between a slope and regular ground, the groundNormal seems to alternate between to values, for example (0, 1, 0) and (0, 0.97, 0.26), wich causes the character to jitter between the two alignments. Here is the script:
void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
isJumpingTimer = 0f;
maxSpeed = baseMaxSpeed;
}
// Update is called once per frame
void Update()
{
float InputX = Input.GetAxisRaw("Horizontal");
float InputY = Input.GetAxisRaw("Vertical");
Vector3 camForward = new Vector3(cam.transform.forward.x, 0f, cam.transform.forward.z);
moveDirection = (camForward * InputY) + (cam.transform.right * InputX);
if (moveDirection != Vector3.zero) //rotate character
{
Quaternion toRotation = Quaternion.LookRotation(Vector3.ProjectOnPlane(moveDirection, transform.up).normalized, transform.up);
rb.rotation = Quaternion.Slerp(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
}
else
{
Quaternion toRotation = Quaternion.LookRotation(transform.forward, transform.up);
rb.rotation = Quaternion.Slerp(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
}
jumpStart();
//Debug.Log(rb.velocity.magnitude);
}
void FixedUpdate()
{
alignToGround();
move();
Jump();
capSpeed();
}
void move()
{
if (onGround)
{
moveDirection = Vector3.ProjectOnPlane(moveDirection, transform.up).normalized;
rb.AddForce(moveDirection.normalized * speed, ForceMode.Force);
}
else
{
rb.AddForce(moveDirection.normalized * speed * airMultiplier, ForceMode.Force);
if (rb.velocity.y > - gravityForce/2)
rb.AddForce(Vector3.down * gravityForce, ForceMode.Force);
}
}
private void jumpStart()
{
if (Input.GetKeyDown(jumpKey) && onGround && !isJumping)
doJump = true;
}
private void Jump()
{
if (doJump)
{
// reset y velocity
rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
doJump = false;
isJumping = true;
isJumpingTimer = 0f;
}
else if (isJumping)
{
isJumpingTimer += Time.deltaTime;
if (isJumpingTimer >= maxJumpTime)
{
isJumping = false;
if (rb.velocity.y > 0)
rb.velocity = new Vector3(rb.velocity.x, smoothJump, rb.velocity.z);
}
if (!Input.GetKey(jumpKey) && isJumpingTimer >= minJumpTime && isJumping)
{
isJumping = false;
if (rb.velocity.y > 0)
rb.velocity = new Vector3(rb.velocity.x, smoothJump, rb.velocity.z);
}
}
}
void capSpeed()
{
if (onGround)
{
if (rb.velocity.magnitude > maxSpeed)
rb.velocity = rb.velocity.normalized * maxSpeed;
}
else
{
Vector3 horizontalVel = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
if (horizontalVel.magnitude > maxSpeed)
{
horizontalVel = horizontalVel.normalized * maxSpeed;
rb.velocity = new Vector3(horizontalVel.x, rb.velocity.y, horizontalVel.z);
}
}
}
void alignToGround()
{
if (isJumping && rb.velocity.y > 0)
{
onGround = false;
changeState();
return;
}
//else
Ray ray = new Ray(transform.position, -Vector3.up);
RaycastHit[] info = Physics.BoxCastAll(rb.position, new Vector3(0.4f, 0.1f, 0.75f), -transform.up, rb.rotation, rayLenght, groundMask);
if (Physics.BoxCast(rb.position, new Vector3(0.4f, 0.1f, 0.75f), -transform.up, rb.rotation, rayLenght, groundMask))
{
Vector3 groundNormal = Vector3.zero;
foreach(RaycastHit r in info)
{
groundNormal += r.normal;
}
groundNormal = groundNormal.normalized;
Debug.Log(groundNormal);
rb.rotation = Quaternion.FromToRotation(transform.up, groundNormal) * transform.rotation;
onGround = true;
//Debug.Log(info[0].point);
}
else onGround = false;
changeState();
Debug.DrawRay(transform.position, -Vector3.up * rayLenght, Color.red);
Debug.DrawRay(transform.position, moveDirection.normalized * 5, Color.blue);
}
void OnDrawGizmosSelected()
{
// Draw a semitransparent red cube at the transforms position
Gizmos.color = new Color(1, 0, 0, 0.5f);
Gizmos.DrawCube(transform.position + new Vector3(0, 2, 0), new Vector3(0.8f, 0.8f, 1.5f));
}
public void changeState()
{
if (onGround)
{
if (moveDirection != Vector3.zero)
rb.drag = groundDrag;
else rb.drag = stillDrag;
isJumping = false;
}
else if (!onGround)
{
rb.drag = airDrag;
}
}
The problem most likely lies in the alignToGround function, but I post the whole script as it may be somewhere else as well.
Also bonus question: how can I check wether BoxcastAll hit without doing another boxcast?