Unity Raycast From Web

PHOTO EMBED

Wed Jan 27 2021 14:56:09 GMT+0000 (UTC)

Saved by @cotterdev #c#

//**** http://gamedevelopertips.com/unity-raycast-2d-what-is-it-and-how-to-use-it/ ****

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    //Variables
    private Rigidbody2D _rb2D;
    List<RaycastHit2D> _hits = new List<RaycastHit2D>();
    ContactFilter2D _myFilter;

    // Start is called before the first frame update
    void Start()
    {
        _rb2D = this.GetComponent<Rigidbody2D>();

        //SEtup because the example we saw said so
        _myFilter = new ContactFilter2D
        {
            useTriggers = false,
            useLayerMask = false    //Not needed, only keeping here in case we want to modify further
        };
        _myFilter.SetLayerMask(LayerMask.GetMask("Player"));    //Sets the raycast to only return hits on the Player
    }

    // Update is called once per frame
    void Update()
    {
        //Draw ray for debug
        Vector2 _here = new Vector2(-3.0f, -3.0f);
        Debug.DrawRay(_here, Vector2.right * 10.0f, Color.red);
    }

    void FixedUpdate()
    {
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            _rb2D.velocity = new Vector2(1.0f, 0.0f);
        }

        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            _rb2D.velocity = new Vector2(-1.0f, 0.0f);
        }

        if (Input.GetKeyDown(KeyCode.Space))
        {
            Vector2 _here = new Vector2(-3.0f, -3.0f);

            Physics2D.Raycast(_here, Vector2.right, myFilter, _hits, 10.0f);

            foreach (var item in _hits)
            {
                Debug.Log($"Collider: {item.collider}");
                Debug.Log($"Tag: {item.collider.tag}");
            }
        }
    }//End FixedUpdate
}//End Class
content_copyCOPY