- Home /
raycast hit point is wrong
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);
}
}
}
Try to give a bit more information. That raycast should hit whatever collider that is in its way. $$anonymous$$ake sure that there is actually a collider there for it to hit. Try debugging the ray using Debug.DrawRay ( ray.origin, ray.direction*500 );
I 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: 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.
thanks. i tryed Debug.DrawRay and attach a separated Capsule Collider. but I cant fix this problem this work like this → http://youtu.be/9Csr7azUeRA and i changed code like this ↓
#pragma strict
function Start () {
}
function Update () {
var cam : Transform = Camera.main.transform;
var ray : Ray = camera.ScreenPointToRay (new Vector3(Screen.width / 2, Screen.height / 2, 0));
var hit : RaycastHit;
Debug.DrawRay ( ray.origin, ray.direction*500 , Color.red);
if(Physics.Raycast (ray, hit, 500)) {
if (Input.Get$$anonymous$$ouseButtonDown(1) && hit.collider.gameObject.tag == "ball") {
print ("enemy!");
Destroy(hit.collider.gameObject);
}
}
}
i cant resolve this problem....
Did 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);
Answer by robertbu · Dec 28, 2013 at 11:28 PM
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);
}
}
}
}
Answer by nastasache · Dec 29, 2013 at 04:03 PM
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.
Answer by mukheion · Dec 29, 2013 at 07:49 PM
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613125003im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
raycast hit point is wrong 1 Answer
making ray ignor certain collider 1 Answer
Obstacle avoidance 1 Answer
Destroy Turret with machine Gun 0 Answers
RayCast Collision between 2 game objects (of the same prefab) 1 Answer