selecting a taged object

ok guys i have searchd the fourms for hours and havent found anything that helps me . my question is that i have a c# script and i am trying to use raycasting to check selected objects

so i want that when the player clicks the right mouse button, if the clicked object is tagged something, and curLevel is greater than Level required ,do another functoion.

i know how to do most of this script but i just have a problem with the ray casting because all i find is java examples -.-" i know people dont like to give out code but any help/example will b good.

Thankyou for taking the time to read.

If you look at the documentation for Physics.Raycast (or other reference pages), you will notice a drop down tab for each example on the right side, probably saying "JavaScript". Press it and select C#. Navigating the documents is a vital skill you must learn.

Here is what the docs say:

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    void Update() {
        RaycastHit hit;
        if (Physics.Raycast(transform.position, -Vector3.up, ref hit))
            float distanceToGround = hit.distance;

    }
}

You can then query hit.transform.tag if it's your appropriate tag.


Since you seem to be having trouble obtaining the information you want, here is a complete script that will debug if a player was clicked or not.

using UnityEngine;

public class RaycastExample : MonoBehaviour
{
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider.CompareTag("Player"))
                {
                    Debug.Log("Clicked Player");
                }
                else
                {
                    Debug.Log("Clicked something else");
                }
            }
        }
    }
}