raycast hit wrong point . this script give to sniper weapon.
raycast does not hit object(add animation)
please help
#pragma strict
function Start () {
}
function Update () {
var cam : Transform = Camera.main.transform;
var ray = new Ray(cam.position, cam.forward);
var hit : RaycastHit;
if(Physics.Raycast (ray, hit, 500)) {
if (Input.GetMouseButtonDown(0) && hit.collider.gameObject.tag == "Enemy") {
print ("enemy!");
Destroy(hit.collider.gameObject);
}
}
}
3 Answers
3
I see nothing wrong with your code that would account for the described behavior. About 50% of the time this issue comes across the list either 1) the name/tag of the game object is not what is expected, or 2) the ray is hitting an unexpected game object. In addition, it is a bit more efficient to place the GetMouseButtonDown() outside of the Raycast(). That way, you don’t do a Raycast() each frame. Try this code. I’ve added a Debug.Log() for the name and the tag.
#pragma strict
function Update () {
var cam : Transform = Camera.main.transform;
var ray = new Ray(cam.position, cam.forward);
var hit : RaycastHit;
if (Input.GetMouseButtonDown(0)) {
if(Physics.Raycast (ray, hit, 500)) {
Debug.Log(hit.collider.name + ", " + hit.collider.tag);
if (hit.collider.tag == "Enemy") {
print ("enemy!");
Destroy(hit.collider.gameObject);
}
}
}
}
Try getting a list of all colliders on front of the camera. Then exclude some unwanted colliders, if any (like your gun collider, for example).
Use different tags for player and head.
The code (in C#, sorry for non-conversion to JS) may look like this:
Raycast.cs:
using UnityEngine;
using System.Collections;
public class Raycast : MonoBehaviour {
void Start () {
}
void Update () {
if( Input.GetButtonDown( "Fire1" )) {
Vector3 start = Camera.main.transform.position;
Vector3 direction = Camera.main.transform.forward;
RaycastHit[] hits = Physics.RaycastAll (start, direction, float.PositiveInfinity);
Debug.DrawRay ( start, direction * 10 , Color.red, 10);
foreach(RaycastHit hit in hits){
Debug.Log("hit.collider="+hit.collider+" hit.point="+hit.point+" tag="+hit.collider.tag);
if(hit.collider.tag == "Player" || hit.collider.tag == "PlayerHead") {
Debug.Log ("Shoot "+hit.collider.tag);
}
}
}
}
}
I did a small test project with this issue, you can try it from here . Look for “HeadCollider” object attached to the head of default Unity 3rd Person Controller.
thankyou. result of trying your code and debug.
There are problems with object(add animation)probably.
I changed different object. It worked
i resolve problem. thanks
Try to give a bit more information. That raycast should hit whatever collider that is in its way. Make sure that there is actually a collider there for it to hit. Try debugging the ray using Debug.DrawRay ( ray.origin, ray.direction*500 );
– half_voxelI have a similar problem (always) with models using Character Controller collider; just I can't shoot enemies on the head because raycast looks founding collider only for 70-80% of character height (up to the neck) - it's what Debug.DrawLine show. If your problem is the same, I think looks like a Unity bug. The only solution I found was to attach a separated Capsule Collider to the head. Having a snipper weapon, good think is attaching that second collider to the head you can count headshots.
– nastasache@nastasache: If you have a similar problem, post another question!. Don't get comment on one that is similar! The likelihood of it being a "Unity Bug" is extremely low and you're probably just doing something wrong.
– Benproductions1Did you put the debug line in I indicated in my answer to see what your Raycast is hitting? Debug.Log(hit.collider.name + ", " + hit.collider.tag);
– robertbu