- Home /
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.
All the code in the documentation is now in all three supported languages. Please read up and make an attempt.
Not quite true :) There are gaps... http://unity3d.com/support/documentation/ScriptReference/Physics.CheckSphere.html for example.
i been through all those, none of them say how to do it with clicking and gametags....
Answer by Statement · Jan 10, 2011 at 11:55 PM
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"); } } } } }
why not? this script do exact what you were looking for... (except that Ger$$anonymous$$ouseButtonDown(1) should be used for the right mouse button) You only have to replace the Debug.Log("Clicked Player") with your stuff and of course you have to replace the "Player" tag with your desired tag string
Bunny83, at the time albert said it didn't help him only the part above the horizontal line was posted. :) I edited the post to add additional information.
Your answer
![](https://koobas.hobune.stream/wayback/20220613094851im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
casting a raycast from the raycast hit.point 1 Answer
find tag from raycast 1 Answer
Make Raycast ignore anything that "Isn't" my player(Solved) 1 Answer
why doesn't this Raycast compare tag work? C# 2 Answers
c# Raycast and then checking tag 2 Answers