- Home /
Using Raycast to detect what's hit and if it's tagged
Hello! I have a class that I'm using in my game to allow the PC to move to the point that the mouse was clicked at (or if held down keep moving toward the mouse cursor.) and now I would like to take it a step further.
I want my class to check if we press the mouse button when positioned over a game object with a specific tag eg. "Enemy" or "Interactive" and then perform my methods depending on what tag the collision has. However I'm not very experienced with coding yet and I'm not sure how to do this.
Can this be done with RaycastHit? If so could someone show me an example or perhaps give me a direction to where I can learn more about it, it'ld be much appreciated! :)
(Below is the Update() in the code I have this far, it's from the click-to-move script found on unify that I just translated to C#)
void Update(){
/// <summary>
/// If the player press the mouse button move the PC toward the point we clicked
/// </summary>>
if(Input.GetKey(KeyCode.Mouse0)){
Plane playerPlane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if(playerPlane.Raycast (ray,out hitdist)){
Vector3 targetPoint = ray.GetPoint(hitdist);
_targetPosition = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = targetRotation;
}
}
//transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * speed);
MoveTowards(_targetPosition);
}
Answer by Ejlersen · Dec 28, 2010 at 07:19 PM
Sure, you can use RaycastHit for this.
Lets take a look at this small example, which is just some of your code, but modified a bit :)
... if(Physics.Raycast(ray, out hitdist)) { string hitTag = hitdist.transform.tag;
if (hitTag == "Enemy")
{
// KILL! CRUSH! DESTROY!
}
else if (hitTag == "Gold Ring")
{
// MY PRECIOUS!
}
} ...
You can use switch-case if you like :)
Here is the documentation for Physics.Raycast:
http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html?from=RaycastHit
For RaycastHit:
http://unity3d.com/support/documentation/ScriptReference/RaycastHit.html (Transform -> GameObject)
Thanks! :D This is on the right track atleast, I got my character to not move if the collider hit was a "monster" tag, however hitdist is a float and don't have a transform so I used a "out RaycastHit" ins$$anonymous$$d. But it's not working just quite how I want it, I tried adding the if(physics.raycast) line inside the playerplane code (inside Input.Get$$anonymous$$ey) this made the PC stop everytime the mouse button was release though, and if put outside the playerplane code the pc stoped properly but when mouse is released and you move the mouse it still moves towards the mouse.
Answer by Kona · Dec 29, 2010 at 07:49 AM
Hm, sorry this didn't get room inside the comment box, anyways this is the code as it looks now,
I'm also curious on how to get the "else if (hit.collider.tag == "Interactive" && distance > 2.0f) {" code to work more properly, as it is now I get the following error. "Operator >' cannot be applied to operands of type
UnityEngine.Vector3' and `float" I suppose the float value I've entered is the problem but how should I define the distance I want in another way?
I also get the error "Cannot implicitly convert type float' to
UnityEngine.Vector3'" from the line where I define the Vector3 "distance".
void Update(){
/// <summary>
/// If the player press the mouse button move the PC toward the point we clicked
/// </summary>>
RaycastHit hit = new RaycastHit();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Input.GetKey(KeyCode.Mouse0)){
Plane playerPlane = new Plane(Vector3.up, transform.position);
// Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitdist = 0.0f;
if(playerPlane.Raycast (ray,out hitdist)){
Vector3 targetPoint = ray.GetPoint(hitdist);
_targetPosition = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = targetRotation;
}
}
//transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * speed);
if(Physics.Raycast(ray, out hit)){
// string hitTag =
// Vector3 distance = Vector3.Distance(hit.transform.position, transform.position);
if (hit.collider.tag == "monster") {
Debug.Log("We are attacking enemy!");
}
// else if (hit.collider.tag == "Interactive" && distance > 2.0f) {
// Debug.Log("We are interacting with a func_ object!");
// }
else{
MoveTowards(_targetPosition);
}
}
// MoveTowards(_targetPosition);
}</code>
Vector3.Distance(v1, v2) returns a float, so you can't put it into a Vector3 :)
If you fix that, then it should also work for your comparison.