- Home /
0 Understanding of raycast2D commands
So I understand what a ray is easy, basically a laser. My problem is I just can not figure out how to use it. Every tuturial I find is just not really helping, so if anyone could either try to explain it, or even share a simple script that just looks forward and logs when it hits somthing, honestly that would be enough for me to be able to figure this out.
Thanks for any help!
here is an example from the official API documentation: https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html
Answer by Zee_pso · Mar 23, 2018 at 08:59 PM
Make sure you're using Physics2D and not Physics. Physics.Raycast will only look for 3d colliders, where Physics2D.Raycast will only look for 2d colliders.
Here's a smaller example.
[code] using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ExampleClass : MonoBehaviour {
// Update is called once per frame
void Update () {
Example();
}
public void Example() {
Vector2 origin = this.transform.position; // Start of the ray
Vector2 direction = this.transform.right; // Ray's direction
float distance = 2f; // How long is the ray? (optional)
LayerMask collisionMask = 1; // What can the ray hit? (optional)
RaycastHit2D hit; // What did the raycast hit?
//hit = Physics2D.Raycast(origin, direction); // To infinity
hit = Physics2D.Raycast(origin, direction, distance, collisionMask); // Only go the exact distance, and only hit whatever is included in the collisionMask.
Debug.DrawLine(origin, origin + (direction * distance), Color.green); // Draws the ray
if (hit) {
print(hit.collider.name); // Print the name of what was hit, if anything was hit.
}
}
} [/code]
Answer by ElijahShadbolt · Mar 23, 2018 at 08:46 PM
Here's a little tutorial.
Create some 2D Sprite GameObjects and add BoxCollider2D components.
GameObject > 2D Object > Sprite
Make sure their Sprite property is set to something so you can see it.
Add Component > Physics 2D > Box Collider 2D
Then create another GameObject, position it below one of the sprites, and attach this script.
Run the scene. When you left-click, the script casts a ray from the object's transform.position
upwards (according to the object's transform.up
). When you right-click, it raycasts into the screen at the mouse position.
Raycast2DTest.cs
using UnityEngine;
public class Raycast2DTest : MonoBehaviour
{
public float maxDistance = 10f; // 10 metres
public LayerMask hitMask = -1; // -1 means hit all layers
public float minZDepth = -1000;
public float maxZDepth = 1000;
// The raycast will hit Collider2D's with a Z value between minDepth and maxDepth.
private void Update()
{
// If left mouse button pressed,
if (Input.GetMouseButtonDown(0))
{
// Raycast upwards from the GameObject.
Vector2 origin = transform.position; // starting point of raycast
Vector2 direction = transform.up; // direction to raycast from origin
// Specify all possible parameters for Physics2D.Raycast()
RaycastHit2D hit = Physics2D.Raycast(origin, direction, maxDistance, hitMask, minZDepth, maxZDepth);
// If something was hit,
if (hit.collider)
{
Debug.Log("Hit: " + hit.collider.name + ", distance: " + hit.distance);
// Draw line in Scene view from `origin` to `hit.point` for 3 seconds.
Debug.DrawLine(origin, hit.point, Color.red, 3f);
// Draw ray in Scene view to represent the `normal` vector for 3 seconds.
Debug.DrawRay(hit.point, hit.normal, Color.green, 3f);
}
else
{
Debug.Log("Nothing hit.");
}
}
// If right mouse button pressed,
if (Input.GetMouseButtonDown(1))
{
// Raycast from where the mouse clicked on the screen.
// Turn mouse screen position to world position.
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// if direction == Vector2.zero, the raycast will be a single point into the screen.
ray.direction = Vector2.zero;
RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction);
if (hit.collider)
{
Debug.Log("Hit: " + hit.collider.name);
}
else
{
Debug.Log("Nothing hit.");
}
}
}
}
Your answer
Follow this Question
Related Questions
Raycast 2D not working as i hoped 0 Answers
Long-distance physics 0 Answers
Rigidbody.position causes shaking 1 Answer
2D Raycast effect only what it hits? 2 Answers
Realistic Bow and Arrow Physics? 2 Answers