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 eoin_a · Sep 06, 2016 at 10:49 AM · raycastingraycasthitraycasts

script attached to main camera wont shoot debug line straight. also wont detect collision on raycast

I have a main camera object with a script attached. I have a child object of the camera, the "crosshairs". which is a transparent cube with a crosshairs image attached. It basically follows the camera and allows the user to direct the raycast to aim at some 3d cubes.

I am also drawing a debug line in the same direction as the raycast to see where iam aiming it. my script is below.

 public float maxdist = 100f;
 public Ray ray;

 void Update () {

     ray = new Ray(gameObject.transform.position, gameObject.transform.forward * maxdist);
     RaycastHit hit;

     Debug.DrawLine(gameObject.transform.position, gameObject.transform.forward * maxdist, Color.green);

     if (Physics.Raycast(ray, out hit, maxdist))
     {
         if (hit.collider.tag == "box")
         {
             hit.transform.SendMessageUpwards("setRunning", true);
             Debug.Log("box hit!!!!");
         }
     }
   }
 }


first, the debug line itself doesnt intersect the center of the crosshairs. so it seems the aim of the crosshairs will be slightly off. I am not sure why. If i add "new Vector3(0,0,1)" instead of Vector3forward. the debugline shoots straight initially but obviously doesnt update. I thought max dist might be the issue here but i removed it and z was even further away from the crosshairs. Also ive tagged my boxes with "box". when i try to pass the raycast through these boxes the "hit" returns "untagged" this is also confusing me. Any help would be really appreciated!

Comment
Add comment · Show 1
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 eoin_a · Sep 06, 2016 at 10:57 AM 0
Share

ive also tried grabbing the camera object in this script (which is attached to the camera). But, the camera object is always null? i used gameObject.GetComponent(); in the Start() method. I am having no luck with this. iam a relatively experinced programmeer but new to unity so I am still learning.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by LarryTheBrave · Jun 04, 2019 at 04:29 AM

The one fishy thing I see so far is looking for the tag on the collider rather than the GameObject. Not sure if there is even such a thing as a collider tag. Alternately try using a built-in tag or even the name instead to find the object, or public GameObject linked to the box itself i.e. public GameObject theBox, then check collider == theBox.

Comment
Add comment · 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

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

70 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 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

Raycast Not Detecting Hit 0 Answers

RayCast appears to bug out for no apparent reason 1 Answer

Raycast help 0 Answers

Raycast is going through collider (A plane) 0 Answers

Switching from object to object with Raycast Problem 1 Answer


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