Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by poppysilvers1997 · Mar 23, 2018 at 07:22 PM · rigidbodyraycastraycasthit2d

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hexagonius · Mar 23, 2018 at 08:11 PM 2
Share

here is an example from the official API documentation: https://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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]

alt text


example.png (14.0 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.");
             }
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges