

#Unity 3d raycast code#
Delete the project, create a new Project and make sure you choose 2D this time. For 3 years now I have been on a journey to recreate game mechanics as a way to learn game development while sharing the code Resources/Tutorial. to the Sprite/Texture.ģ.If #2 did not work, then your project was created as a 3D instead of 2D. Since this is a 2D game, any collider you are using must end with 2D.For example, there is a Box Collider and there is a Box Collider 2D. Make sure that the Collider is covering the 2D Texture or Sprite by re-sizing the collider. There are other ways, like using vectors and angles, but in this case I’ve chosen ray casting because it’s powerful, easy to use and.

We need to detect if the monolith is in front of the player, so first of all we need to detect somehow which object is in his front. RaycastHit2D cubeHit = Physics2D.Raycast(cubeRay, Vector2.zero) ĭebug.Log("We hit " + ) įor this to work, you must attach Collider2D to the 2D Texture or Sprite. Basic raycasting in Unit圓D monolith ahead Basic but useful Ray casting. Public void OnPointerDown(PointerEventData eventData)Ģ.Now if the GameObject you want to detect the touch with is just a 2D Texture or Sprite then use the code below: if (Input.GetMouseButtonDown(0)) Public void OnPointerClick(PointerEventData eventData) public class YourClass : MonoBehaviour,IPointerDownHandler,IPointerClickHandler To detect touch with Image/Canvas, you use have to derive from IPointerDownHandler or IPointerClickHandler then implement the functions from them.
#Unity 3d raycast how to#
To learn more about using Raycasts in a 2D scene see the documentation for Physics2D.Raycast.1.If the Object you are trying to detect touch with is an Image/ Canvas, then this is not how to do this.

Physics.Raycast will interact with 3D Colliders, Physics2D.Raycast will interact with 2D Colliders. These have a different syntax and the two raycasting systems are not intercompatible. Variants of the 3D Raycasts and its alternatives can be used when your using Unity’s 2D Physics system. Doing a linecast or raycast directly between the object and the target to check if anything is in the way is better. Either way, as Farfarer said, thats really not a good way to do a visibility test. Youd probably want Physics.OverlapSphere for that. This functions identically to a Raycast however instead of expecting an origin and direction and LineCast expects two points and detects if an object is between those two points. SphereCast is a fat, tubular raycast, not really a raycast in all directions. CapsuleCastĬapsuleCast projects a capsule along a ray and can be useful for detecting things like potential player movement. BoxCastīoxcast projects a box along the ray and allow you to test if a box may move without colliding with an object. This has a few uses but one of the most common is in the development of third person cameras to prevent them from clipping into level geometry and ensure they behave more cleanly in tight corners. SphereCast projects a sphere along a ray effectively allowing your raycast to have a radius around itself when detecting objects. These function similarly to raycasts for the most part besides having a different shape.

Unity includes a set of functions that can be used in place of raycasts that use different shapes such as spheres or boxes to detect collisions.
