- Home /
Edge/Corner Stick — issue or logical error?
Hi! I use the following code to move a hero:
void Update () {
Move ();
UpdateRaycast();
}
void Move () {
movementDirection = Vector3.zero;
if (isLeft() && !isBlockedLeft) { movementDirection.x = -1; } if (isRight() && !isBlockedRight) { movementDirection.x = 1; }
if (isUp() && !isBlockedUp) { movementDirection.y = 1; } if (isDown() && !isBlockedDown) { movementDirection.y = -1; }
if (!isFalling)
{
movement = new Vector3 (movementDirection.x, movementDirection.y, 0);
movement *= Time.deltaTime*moveSpeed;
gameObject.transform.position += movement;
}
if (isFalling)
{
movement = new Vector3(0, -1, 0);
movement *= Time.deltaTime*moveSpeed;
gameObject.transform.position += movement;
}
}
void UpdateRaycast() {
float _x = this.transform.position.x;
float _y = this.transform.position.y;
float _z = this.transform.position.z;
RaycastHit hit = new RaycastHit();
// raycast from left and right side to down
if (Physics.Raycast(new Vector3(_x - this.transform.collider.bounds.size.x/2, _y, _z), Vector3.down, out hit, this.transform.collider.bounds.size.y/2) ||
Physics.Raycast(new Vector3(_x + this.transform.collider.bounds.size.x/2, _y, _z), Vector3.down, out hit, this.transform.collider.bounds.size.y/2))
{
isBlockedDown = true;
} else { isBlockedDown = false; }
// raycast from left and right side to top
// raycast from top and down side to left
// raycast from top and down side to right
}
(I cut three pieces of raycast, they're the same as the first one). Everything seems to be ok in logic, but when i move my hero, he absolutely tight stumbles on the corners or edges. You can see the sample movie i made.
I add
this.transform.position = new Vector3 (_x, hit.transform.position.y + this.collider.bounds.size.y + 1, 0);
to the first piece (// raycast from left and right side to down). And stumbling disappeared. But i got a 1 px gap and unwanted braking when i hold /Left/+/Down/ or /Right/+/Down/. Here is the sampe.
It seems like rounding problems because if i remove +1 from line above i get stumbling again.
Any suggestions?
Your answer
![](https://koobas.hobune.stream/wayback/20220613093038im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can someone explain why my Raycasting doesn't work? please :) 2 Answers
Problem with raycast detection? 1 Answer
raycast in direction of movement key down 2 Answers
RaycastHit always returns 0 1 Answer