Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Paum · Nov 08, 2011 at 12:10 AM · raycastraycasthitdrawray

Raycast and drawray problems

Hello... Im making a FPS on C# and im having some problems with the Raycast.

  Vector3 fwd = direction.transform.position;
             RaycastHit hit;

             if (Physics.Raycast(fwd, transform.forward, out hit)) ;
             {
                 Debug.DrawRay(fwd, transform.forward, Color.red, 5F);
                 if (hit.transform.gameObject.tag == "Wall")
                 {
                     print("HITwall");
                 }

                 if (hit.transform.gameObject.tag == "Enemy")
                 {
                     //print("HITenemy");
                    GameObject.FindWithTag("Enemy").GetComponent<FSMSimples>().DmgOn = true;
                 }

The draw should begin at the direction position (the direction is an empty gameobject attached to the gun), but its starting far away from it.

Error

The red line is the draw from the Debug.DrawRay. Can someone help?? I dont know what to do. Tnks!

Comment
Add comment · Show 2
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 syclamoth · Nov 08, 2011 at 12:34 AM 1
Share

Which object is 'direction'? Why don't you just use

 Physics.Raycast(transform.position, transform.forward, out hit)

$$anonymous$$ake sure you aren't getting any of the parameters mixed up.

avatar image Paum · Nov 08, 2011 at 04:22 PM 0
Share

I need the start point at the gun, not at the player

2 Replies

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

Answer by Owen-Reynolds · Nov 08, 2011 at 01:05 AM

There's a semi-colon after your if. That makes the lines after it not part of the IF, so they always run. If should be spraying errors about "hit.transform is NULL" after each miss.

The short red lines are usual. Unlike raycasts, Debug.DrawRay has the direction and distance together (transform.forward*10 will make a longer line.)

I'd guess the code is otherwise working perfectly, but your direction empty is in the wrong spot. Try putting a cube (with no collider) on it, for a visual, or just select it while running in Scene view, or check every rotation while you're running. You might see something with the blue-arrow unexpectedly going down, putting "far forwards" exactly where the red lines are coming from.

[Edit: new from here down] To really go nuts, check whether direction is what you think it is (make it a public global & check inspector while running.) Try printing Debug.Log("fwd="+fwd); to make sure fwd isn't really underground.

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
avatar image
0

Answer by Paum · Nov 08, 2011 at 05:14 PM

Thanks, I had not seen the semi-colon there.

Now the Direction is selected, and i dont know why the Ray is so far from it. direction

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to place an object in the direction of a raycast 1 Answer

Raycast won't hit Collider 1 Answer

Make Raycast ignore anything that "Isn't" my player(Solved) 1 Answer

Wall running using raycasting. 2 Answers

Layermask doesn't seem to work 2 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