- Home /
Raycast2D Only returns true
I'm bulding a simple 2D platformer but whenever I raycast it only returns true. Even when there is no object as i used Debug.DrawRay and saw the ray was hitting nothing but still the raycast was still returning true even as my player is under all of the Coliders.
using UnityEngine;
using System.Collections;
public class Controls : MonoBehaviour {
[HideInInspector] public bool FacingRight;
public float Speed = 6f;
public float RunSpeed = 12f;
public float JumpDistance = 1f;
Animator playerControls;
Collider2D playerBox;
Rigidbody2D playerRigidbody;
Vector2 positonToMove;
float distToGround;
void Start () {
playerControls = GetComponent<Animator> ();
playerRigidbody = GetComponent<Rigidbody2D> ();
playerBox = GetComponent<BoxCollider2D> ();
distToGround = playerBox.bounds.extents.y;
}
void Update () {
float x = Input.GetAxisRaw ("Horizontal");
Move (x, 0);
if (Input.GetKey("z") && IsGrounded() == true )
{
Jump();
}
else if(Input.GetKey("x"))
{
Debug.Log(IsGrounded().ToString());
}
Animate (x,0);
}
void Move(float x, float y)
{
positonToMove.Set (x, y);
if (playerControls.GetBool ("IsRunning") == true) {
Speed = RunSpeed;
}
else {
Speed = 6f;
}
positonToMove = positonToMove.normalized * Speed * Time.deltaTime;
playerRigidbody.MovePosition ((Vector2)transform.position + positonToMove);
}
void Jump()
{
playerRigidbody.AddForce(new Vector2(0f, JumpDistance* 5));
}
void Animate(float x, float y)
{
bool isWalking = x != 0f;
bool isRunning = x == 1f || x == -1f;
bool isJumping = y != 0f;
bool isFalling = playerRigidbody.velocity.y < -0.1f;
playerControls.SetBool ("IsWalking", isWalking);
playerControls.SetBool ("IsRunning", isRunning);
playerControls.SetBool ("IsJumping", isJumping);
playerControls.SetBool ("IsFalling", isFalling);
}
bool IsGrounded()
{
RaycastHit2D myHit;
int Layermask = 1 << 8;
Layermask = ~Layermask;
Debug.DrawRay(transform.position, -(Vector2)Vector3.up, Color.red, 0.1f );
bool p = Physics2D.Raycast ((Vector2)transform.position, -Vector2.up, /*distToGround*/ 0.1f, Layermask);
if (p == true) {
Debug.Log ("T");
} else {
Debug.Log("F");
}
return p;
}
}
thank you for telling me how to do that i should have realized the ones and zeros were for highlighting code
ray can return the object's collider( from which its cast, if cast from a point inside of that collider). Have you ignored that collider?
I might have missused the bitwise shift and reversed it saying check only layer 8 when I should have said simply shift 8
Answer by nullgobz · Aug 01, 2015 at 12:05 PM
Your testing if the RaycastHit2D is not null and it never is. Try this:
RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up, 0.1f, Layermask);
if(hit.collider != null)
{
print("HIT");
}
About bitswift, yeh your checking against all layers except for layer 8. And if layer 8 is ur character this makes sense so you dont check against yourself.
Ya I just realized that right after I posted it and thanks I will try that
Your answer
Follow this Question
Related Questions
Still constant jumping even with raycasting. 0 Answers
ground check isnt working...HELP! 2 Answers
Player Inconsistent Jumps 0 Answers
Player sticks to ground occasionally after trying to jump 0 Answers
Anti-Gravity isn't working 1 Answer