Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by infilrtrator_55 · Oct 21, 2015 at 03:13 AM · c#rotationcollisionraycast

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): alt text 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: alt text 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.

globalz.png (14.9 kB)
localz.png (14.7 kB)
Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image toromano · Oct 21, 2015 at 07:20 AM 0
Share

Did you checked if empty gameobject and BarrelTip have same direction aligned pivot points?

avatar image infilrtrator_55 toromano · Oct 21, 2015 at 08:06 AM 0
Share

@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.

avatar image toromano infilrtrator_55 · Oct 21, 2015 at 08:13 AM 0
Share

Sorry for missunderstanding. Did you checked z positions of BarrelTIp and enemy? $$anonymous$$aybe there is an offset in z.

Show more comments
avatar image toromano · Oct 21, 2015 at 09:17 AM 0
Share

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

avatar image infilrtrator_55 toromano · Oct 21, 2015 at 10:24 AM 0
Share

@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?

avatar image toromano infilrtrator_55 · Oct 21, 2015 at 10:34 AM 0
Share

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);
                  }
            }
      }


Show more comments
avatar image CanCo · Oct 21, 2015 at 10:29 AM 0
Share

I'm writing a code now, checking if I can get it to work.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image toromano · Oct 22, 2015 at 05:40 AM 0
Share

That's a relieve! :D

avatar image CanCo · Oct 22, 2015 at 05:49 AM 0
Share

You don't need to do "barrel_tip.transform.LookAt(enemy.transform)" if they're transforms already. Just do "barrel_tip.LookAt(enemy);".

avatar image
1

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+")");
     }
 
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image infilrtrator_55 · Oct 21, 2015 at 12:52 PM 0
Share

@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.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image infilrtrator_55 · Oct 21, 2015 at 01:44 PM 0
Share

@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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

39 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotate camera on collision of character 1 Answer

create object on raycast collision that follows raycast. 0 Answers

How can I make a third person camera collision script? 2 Answers

Raycast does not seem to properly detect object it hit 0 Answers

Raycast problem/wall climb system 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges