RaycastHit2D not getting any information?

Hi again.

I try to make a character movement and control out of raycasting but now I got into a problem.

Its not hitting anything although there is a collider there.
![alt text][1]

![alt text][2]

And this is the script:

using UnityEngine;
using System.Collections;

public class Controller : MonoBehaviour {

	public float OnGround = 2.5f;

	//Planing to add ((public Rigidbody2D rb2D;))
	void Start() {
		//Planing to add ((rb2D = GetComponent<Rigidbody2D>();))
	}
	void FixedUpdate() {
		transform.Translate (Vector2.down * 0.1f);

		RaycastHit2D hit = Physics2D.Raycast(this.gameObject.transform.position, Vector2.down);
		if (hit.distance <= OnGround) {
			print ("Stop!");
			transform.Translate (Vector2.zero);
		}
	}
}

As I said it doesn’t detect the collider. any ideas?

Thanks for reading.
-Coaster Mind

Solved!

The transform.Translate (Vector2.down * 0.1f); was first without a else so the script didn’t know that it did need to stop it when the ray gave information to do it.

Updated and working script: RaycastHit2D hit = Physics2D.Raycast(this.gameObject.transform.position, Vector2.down); if (hit.distance <= OnGround) { print ("Stop!"); transform.Translate (Vector2.zero); } else transform.Translate (Vector2.down * 0.5f);

Hello,

As it turns out, 3d Raycasting does not work with 2D colliders. A good alternative to raycasting could be to add a 2D collider to your character (or 2dTrigger, depending on how you plan on doing it).

Hope this works :slight_smile: