unity raycast 2d

// detect object that was clicked using raycast

RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
 
if(hit.collider != null)
{
    Debug.Log ("Target name: " + hit.collider.name);
}

3.8
5
David 95 points

                                    using UnityEngine;public class Example : MonoBehaviour
{
    // Float a rigidbody object a set distance above a surface.    public float floatHeight;     // Desired floating height.
    public float liftForce;       // Force to apply when lifting the rigidbody.
    public float damping;         // Force reduction proportional to speed (reduces bouncing).    Rigidbody2D rb2D;
    void Start()
    {
        rb2D = GetComponent<Rigidbody2D>();
    }    void FixedUpdate()
    {
        // Cast a ray straight down.
        RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up);        // If it hits something...
        if (hit.collider != null)
        {
            // Calculate the distance from the surface and the "error" relative
            // to the floating height.
            float distance = Mathf.Abs(hit.point.y - transform.position.y);
            float heightError = floatHeight - distance;            // The force is proportional to the height error, but we remove a part of it
            // according to the object's speed.
            float force = liftForce * heightError - rb2D.velocity.y * damping;            // Apply the force to the rigidbody.
            rb2D.AddForce(Vector3.up * force);
        }
    }
}

3.8 (5 Votes)
0
4.57
7
Aduku 135 points

                                    Physics2D.Raycast(Vector2 origin, Vector2 direction, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity);

4.57 (7 Votes)
0
4
1

                                    using UnityEngine;public class Example : MonoBehaviour
{
    // Float a rigidbody object a set distance above a surface.    public float floatHeight;     // Desired floating height.
    public float liftForce;       // Force to apply when lifting the rigidbody.
    public float damping;         // Force reduction proportional to speed (reduces bouncing).    Rigidbody2D rb2D;
    void Start()
    {
        rb2D = GetComponent<Rigidbody2D>();
    }    void FixedUpdate()
    {
        // Cast a ray straight down.
        RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up);        // If it hits something...
        if (hit.collider != null)
        {
            // Calculate the distance from the surface and the "error" relative
            // to the floating height.
            float distance = Mathf.Abs(hit.point.y - transform.position.y);
            float heightError = floatHeight - distance;            // The force is proportional to the height error, but we remove a part of it
            // according to the object's speed.
            float force = liftForce * heightError - rb2D.velocity.y * damping;            // Apply the force to the rigidbody.
            rb2D.AddForce(Vector3.up * force);
        }
    }
}

4 (1 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
physics 2d raycast unity unity raycast 2d mouse position 2d unity raycast from camera raycast from camera unity 2d raycast in 2d unity raycast2d mouse position unity image raycast 2d unity 2d raycast hit unity how to do raycasting 2d 2d physics raycast in unity 2d raycast unity raycast 2d point make raycast in unity 2d raycast 2d object physics 2d raycast how to send out a raycast from the mouse unity 2d raycast right unity 2d how to raycast 2d unity physics 2d raycast how to use a raycast 2d in unity from a game object how to use a raycast 2d in unity unity raycast on 2d raycasting in unity 2d unity 2d raycast from camera unity 2d raycast to mouse pos unity raycast 2d how to see it raycast in unity 2d hw to raycast in unity 2d raycast 2d in unity unity 2d raycast at mouse position raycasts unity 2d raycast 2d with mouse unity mouse raycast 2d Unity 2d raycasts raycast 2d mouse position raycast mouse position unity unity 3d 2d raycast how to cast 2d ray in unity how to use raycast unity 2d how to make raycast in unity 2d Define that position of raycast unity 2d unity 2d point raycast raycast script unity 2d how to declare a raycast 2d unity unity show raycast 2d raycast 2d raycast 2d unity c# unity 2d Physics2D.Raycast unity 2d mouse raycast unity 2d raycast c# unity raycast 2d mouse unity raycast physics 2d unity ray cast hit 2d unity 2d raycasty unity 2d raycast mouse position raycast documentation unity 2d raycast hit 2d unity how to use raycasting unity 2d raycast mouse position unity 2d Raycast all unity 2d unity 2d draw raycast unity 2d raycasthit2d unity 2d raycast 2d 2d raycast unity raycast 2d parameters unity raycast 2d unity unity raycast in 2d game 2d raycasting physics 2d raycast mouse position raycast 2D unity variables unity raycast unity 2d raycast in mouse direction draw raycast 2d unity unity raycast mouse position 2d raycast 2d unity c# raycast hit 2d unity 2d raycast mouse hover unity 2d raycast mouse unity 2d raycast down raycast unity 2d unity casting 2d raY unity 2d raycast collision unity 2d raycast unity raycast hit 2d unity add 2d raycast unity raycast 2d see player unity raycast 2d shoot raycast in unity 2d using physics 2d .raycast unity unity easy 2d raycast unity send raycast 2d unity raycast 2d mouse click 2d raycast from camera unity 2d rays raycast 2d unity example physics raycast 2d unity2d raycast mouse how to give a thickness value to the raycast in unity2e how to fix the raycast is colliding with its gameObject in unit2d raycast in unity2d unity racast2d 2D raycasts unity raycast a gameobject unity 2d raycast 2d gameobject unity what is raycast 2d physics raycast unity 2d racsting 2d objects understanding raycast 2d ray 2d unity3d unity raycasts2d how to raycast a 2d object in unity unity raycast for 2d objects on scene Unity on collision 2d raycast 2d out hit unity raycast mouse unity raycasting in 2d how to 2d ray cast unity unity raycast from mouse 2d raycast from mouse 2d raycast from finger touch javascript 2d raycast colitions how to reference object with physics2d.raycast unity unity how to use raycast2d unity documentation physics2d.raycast unity get a 2d ray from mouse position raycast collision 2d unity raycsat unity 2d unity raycast mouse physics2d 2D raycsts unity raycasting 2d physics 2d raycast hit raycast 2d laserlength unitiy C# raycast 2d unitiy C# raycast2d unity C# raycast 2d up and right unity 2d raycast towards screen unity 2d raycast towards camera unity2d raycasting panel raycast 2d unity mouse unity raycast 2d collision can you fire rays in 2d unity unity2d shoot ray unity 2d raycast on hit Physics2D.Raycast reverse unity2d raycast physics2D unity hit raycast 2d directions 4 WAY raycast physics2d unity raycast physics2d unity raycast in 2d unity unity reaycast from vector2 why ist raycast2d hitting unity unity how to do a 2d raycast how to make 2d raycasting how to cast ray2d forward untiy Raycast 2d C# raycast on 2d element unity if physics2d raycast raycast 2d hit unity unity raycast 2d hit example 2d raycasts taycast 2d 2D RAY CAST out for physics2d 2d raycasting unity unity raycast 2d taille how to use transfrm[] in a raycast 2d unity ray casting in unity2d unity 2d raycaster cast a ray 2d unity physics2d.raycast in unity unity 2d raycast collision detection raycasting collision detection 2d unity unity raycast 2 raycasting2d unity tutorial unity physics2D.Raycast unity 2d ray unity 2s ray raycast2d unity how to use raycasts unity 2d physics2d.raycast out hit how to use raycasts in unity 2d physics2d.raycast raycast 2d check how to make a 2d raycast in unity Raycasting 2D unity unity raycast2d how to raycast unity 2d raycasting unity 2d physics raycast 2d unity send a 2draycast down from object unity physics2d.raycast unity unity physics2d raycast raycast - unity 2d unity raycast 2d
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source