- Home /
Raycast Does not Detect Collider
Probably going to feel stupid after getting an answer, but 4 hours of looking isn't getting me anywhere. I have a player move script that is supposed to send a raycast and not allow movement if the raycast hits a collider. However, no matter what, the raycast does not detect any colliders (the integer is always 0)
Here is my code: using UnityEngine; using System.Collections;
namespace Player.control {
public class PlayerMove : MonoBehaviour {
[SerializeField] private float speed = 0.25f;
[SerializeField] private Rigidbody2D rb2d;
[SerializeField] private BoxCollider2D bc2d;
void Update () {
if (false) {
// Switches colliding list
} else {
// Moves the player
float x = Input.GetAxis ("Horizontal") * speed;
float y = Input.GetAxis ("Vertical") * speed;
RaycastHit2D[] results = {};
if (bc2d.Raycast (new Vector2 (x, y),
results, 1.2f, Physics2D.AllLayers, Mathf.NegativeInfinity, Mathf.Infinity) != 0)
return;
rb2d.MovePosition (new Vector2 (this.transform.position.x + x, this.transform.position.y + y));
}
}
}
}
For one thing; Physics.Raycast returns a boolean value, so you don't have to check "!= 0". Just as a failsafe, try moving the move function call into an else statement under your raycast/return.
This is not Physics.Raycast (3D) but Collider2D.Raycast (2D) and it returns an int containing the number of results returned.
Answer by turndapage · Apr 06, 2017 at 02:21 PM
Maybe try a different approach. Here is a script I just wrote that works for me:
void CheckForObstacle()
{
// Looks ahead of character for obstacle
Debug.DrawRay(transform.position, fwd * viewDistance);
RaycastHit2D hitSolid = Physics2D.Raycast(transform.position, fwd, viewDistance, 1 << LayerMask.NameToLayer("Solid"));
if (hitSolid)
{
// If a collision was made, make sure the enemy is patrolling and move to the next position after it reaches the obstacle.
status = Status.patroling;
if (IsAtPosition(hitSolid.transform.position))
currentPoint++;
}
}
The layer "Solid" are objects that should block the character. I believe you should just use Mathf.Infinity and make sure the direction vector is facing the towards were the character is going. Also, you probably don't need to make an array of hits as it will always just return the first collider it finds.
Thanks, I used a modified version of this code and it worked :D
Answer by MelvMay · Apr 05, 2017 at 06:53 AM
Here's the documentation: https://docs.unity3d.com/ScriptReference/Collider2D.Raycast.html
Here it states:
This function is similar to the [[Physics2D::RaycastNonAlloc]] function and in the same way, the results are returned in the supplied array. The integer return value is the number of objects that intersect the ray (possibly zero) but the results array will not be resized if it doesn't contain enough elements to report all the results. The significance of this is that no memory is allocated for the results and so garbage collection performance is improved when raycasts are performed frequently.
You pass in an empty array therefore you get zero results back.
Also note, you do not need to pass in the Physics2D.AllLayers, Mathf.NegativeInfinity, Mathf.Infinity
as they are use the same values by default as the documentation shows.
$$anonymous$$ind of confused why this wasn' t best answer being as it's the reason why your code didn't work and wasn't returning result.
Can confuse others who look at posts.
The code that I had didn't use the returned array, but rather uses the integer result.
RaycastHit2D[] results = {};
if (bc2d.Raycast (new Vector2 (x, y),
results, 1.2f, Physics2D.AllLayers, $$anonymous$$athf.NegativeInfinity, $$anonymous$$athf.Infinity) != 0)
Your code passed in an empty array and would therefore never return any results whether you used them or not. It would obviously also always return zero (no results) no matter what which is what my answer points out.The code that I had didn't use the returned array, but rather uses the integer result.
Your answer
Follow this Question
Related Questions
Scroll view not scrolling when colliders are in the way 0 Answers
2D raycast to UI problem 1 Answer
Raycast doesn't work on edge colliders in Unity 5.5.4 0 Answers
Cutting a Sprite 0 Answers
Is my 2D Raycast set up correctly? because it does not seem to hit anything when i use a layer mask. 1 Answer