- Home /
Question by
SOLIDDUKEY · Apr 04, 2018 at 10:09 PM ·
raycast2d gameraycasting2d-gameplayraycasthit2d
What's wrong with my RayCastHit2D. Raycast only on positive values on horizontal axis and on negative values on vertical axis
The player only stops moving when moving horizontally when it meets blocking layer or actor (using d), but does not stop when moving horizontally negatively (using a). When moving vertically, the player only stops when moving negatively (using s) but not when moving positively. I've attached the snippet of code i have, does anyone see what I am missing or doing wrong?
hit = Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(Input.GetAxisRaw("Horizontal"), 0), Mathf.Abs(Input.GetAxisRaw("Horizontal") * Time.deltaTime),
LayerMask.GetMask("Actor", "Blocking"));
if (hit.collider == null)
{
if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
{
transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * movementSpeed * Time.deltaTime, 0f, 0f));
playerMoving = true;
lastMove = new Vector2(Input.GetAxisRaw("Horizontal"), 0f);
}
}
hit = Physics2D.BoxCast(transform.position, boxCollider.size, 0, new Vector2(0, Input.GetAxisRaw("Vertical")), Mathf.Abs(Input.GetAxisRaw("Vertical") * Time.deltaTime),
LayerMask.GetMask("Actor", "Blocking"));
if (hit.collider == null)
{
if (Input.GetAxisRaw("Vertical") > 0.5f || Input.GetAxisRaw("Vertical") < -0.5f)
{
transform.Translate(new Vector3(0f, Input.GetAxisRaw("Vertical") * movementSpeed * Time.deltaTime, 0f));
playerMoving = true;
lastMove = new Vector2(0f, Input.GetAxisRaw("Vertical"));
}
}
capture.png
(43.1 kB)
Comment