Rotated object raycasting in wrong directions!!?
I have a empty parented to a gun's tip. I have a fire script attached to the gun for raycasting(detecting enemies), instantiate my bullets and emit particles from the empty. The problem is when the gun is rotated in its Y-axis the empty also rotates and raycasts in wrong directions. Here is the code:
void Start()
{
barrel_tip = transform.GetChild(1);
}
RaycastHit hit;
void Update()
{
Collider[] colliders = Physics.OverlapSphere(transform.position, 30);
for (int i = 0; i < colliders.Length; i++)
{
if (colliders[i].tag == "Enemy")
{
enemy_list.Add(colliders[i]);
}
}
if (enemy_list != null)
{
foreach (Collider enemy in enemy_list)
{
if (Physics.Raycast(barrel_tip.position, barrel_tip.transform.forward, out hit, 50))
{
Debug.Log(hit.distance);
if (hit.distance < range)
{
print("Target in range.");
anim.SetBool("enemy_det", true);
}
}
}
}
}
After the gun is rotated the empty raycasts in global Z-axis something like this(red line is the raycast): As you can see its not hitting the cube even when the gun is facing the cube and I'm using barrel_tip.forward. But I want to raycast in the local Z-axis of the empty so that it hits the target(blue cube) like this: In many similar questions people say use transform.forward instead of Vector3.forward but this doesn't solves my problem. I want a way to be able to raycast in the local Z-axis. When the gun is rotated empty raycasts in global Z direction and hence doesn't detects the enemy gameobject. Please help.
Edit: The script iterates through the enemy_list as there can be many enemies in the range. I'm guessing this is the root of my problem. You can get the full script here.
Did you checked if empty gameobject and BarrelTip have same direction aligned pivot points?
@toromano the name of the empty gameobject IS 'BarrelTIp' and I'm raycasting through that empty gameobject though the script is placed in parent gun object.
Sorry for missunderstanding. Did you checked z positions of BarrelTIp and enemy? $$anonymous$$aybe there is an offset in z.
Empty rotattions are relative rotations. So it shows empth gameobject is rotating with parent. How about position value of empty gameobjects. it has to be (0,0,0) too
@toromano Currently position of empty is (0, 0, 8), if I set it to 0 the empty gets too behind the barrel. You think this is the reason what should I do?
Is this possible for you to debug that line before raycasting using:
void Update()
{
Debug.DrawRay(barrel_tip.position, barrel_tip.forward, Color.green);
if (Physics.Raycast(barrel_tip.position, barrel_tip.forward, out hit, 100))
{
///print(hit.distance);
if (hit.distance < range)
{
//Some code here...
anim.SetBool("enemy_det", true);
}
}
}
Answer by infilrtrator_55 · Oct 22, 2015 at 05:37 AM
Haha, Finally found a fix after 1 day of head smashing. The key is to rotate the empty object towards the enemy Gameobject then raycasting through the empty. I do this by Transform.LookAt() before the raycast.
barrel_tip.transform.LookAt(enemy.transform);
if (Physics.Raycast(barrel_tip.position, barrel_tip.forward, out hit, range))
{
anim.SetBool("enemy_det", true);
}
It works flawlessly now. Thanks @CanCo and @toromano for all the help.
You don't need to do "barrel_tip.transform.LookAt(enemy.transform)" if they're transforms already. Just do "barrel_tip.LookAt(enemy);".
Answer by CanCo · Oct 21, 2015 at 10:47 AM
I wrote this and it worked perfectly fine:
It projects a white line so you can see where the line is going, prints to the console so you know what it's hitting (and got a little bored so I also added in the normal). The white line is the length of the range so it should be easier to find an error.
Also, I should note, I commented out the automatic finding of the barrel tip because that may or may not be an error.
using UnityEngine;
using System.Collections;
public class RaycastProblem1 : MonoBehaviour {
public Transform barrel_tip;
public float range = 25f;
void Start()
{
// barrel_tip = transform.Find("BarrelTip");
}
void Update()
{
Debug.DrawLine(barrel_tip.position,
barrel_tip.position + (barrel_tip.forward * range)
);
RaycastHit hit;
if (Physics.Raycast(barrel_tip.position, barrel_tip.forward, out hit, range))
{
Debug.DrawLine(hit.point,
hit.point+hit.normal);
DoSomething(hit);
}
}
void DoSomething (RaycastHit hit)
{
Debug.Log("I can see you! ("+hit.transform.name+")");
}
}
@CanCo Thanks! But for me both debug.DrawLine() draw nothing on the screen. And I still think raycast is cast in a wrong direction. I think the problem is I'm doing this inside a foreach statement in the update function. I'm editing the question take a look.
Answer by toromano · Oct 21, 2015 at 01:34 PM
infilrtrator_55 If your gun has a collider and your BarrelTip gameobject is inside that collider, your Raycast function will return false. Also your ray could hit your gun's collider or someplace else if you parented your BarrelTip gameobject wrongly.
@toromano This is going to be a top down game. And I've checked the gun's collider is far behind the empty gameobject. No collider is overlapping the empty.