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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by husbandofemily · Apr 22, 2013 at 07:01 PM · raycastairaycasthit

Raycast points to world center

Hi all, got a bizarre one I just can't figure out. A bit of background to make it make sense: This is for an AI script, a coroutine in JS that simulates a gun firing. "Spinner" is the child gameObject that had a LookAt script to make it look at it's target, then the character Slerps to Spinner's rotation. When tgt is an enemy and the rotation is right, fire() coroutine is called. This much works. I have a raycast in the fire() coroutine, I want it to raycast from muzzle in the direction of spinner, and instantiate an object at the raycast hit.

For some reason though, the raycast goes to 0,0,0 world space, even though it's miles away. I've proved this with Drawline, and the object instantiates correctly at world center. I just can't figure out why?!? Here's the coroutine, excuse the lack of formatting, this computer I'm on won't allow the format button to work from some reason:

 function fire()
 {
     var coll : RaycastHit;
     var spinFwd = spinner.transform.TransformDirection(Vector3.forward);
     if(Physics.Raycast(muzzle.position, spinFwd, 20)
     {
         Instantiate(prefab, coll.point, transform.rotation);
         Debug.Drawline(muzzle.position, coll.point);
     }
 }

What's wrong with this???? It's driving me mad! 0,0,0 is miles away, certainly not within 20 units, and not in the direction "spinner" is looking. I tried it in Update() with the same results. Any help greatly appreciated.

Regards Ian

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 AlucardJay · Apr 22, 2013 at 07:38 PM 0
Share

Debug where you are casting the ray :

 var rayDistance : float = 1000.0;
 var spinFwd = spinner.transform.TransformDirection(Vector3.forward);
 Debug.DrawRay( muzzle.position, spinFwd * rayDistance, Color.yellow );
 if ( Physics....

you may also want to put a time in for how long the DrawRay is visible :

 Debug.DrawRay( muzzle.position, spinFwd * rayDistance, Color.yellow, 0.5 );

http://docs.unity3d.com/Documentation/ScriptReference/Debug.DrawRay.html

Edit : in general, why do some people use TransformDirection?

1 Reply

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

Answer by husbandofemily · Apr 22, 2013 at 07:53 PM

OK everyone, I appreciate the help, but I figured it out eventually, and now feel stupid... It's

if(Physics.Raycast(muzzle.position, spinFwd, coll, 20));

I didn't put the "Coll" in the Raycast line. I don't understand why this works, or what it affects, since in the docs first example doesn't use the RaycastHit variable. I'll leave this up in case anyone else has this.

Thanks again guys :-)

Ian

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 Julien-Lynge · Apr 22, 2013 at 10:56 PM 0
Share

$$anonymous$$arked as accepted to close the question. Glad you got it figured out.

As for why adding 'coll' causes it to work: When you create the coll variable, you're creating a new struct. By default, all its values with be whatever the default is for their type (e.g. the point will be (0,0,0), the distance will be 0, etc.). When you pass Coll to the overloaded Raycast method, it fills in the Coll struct with the information from the collision. Only if you do so will Coll have real data.

If you're not familiar with structs and overloading, take a moment to google them - they're quite useful terms to know.

avatar image husbandofemily · Apr 22, 2013 at 11:04 PM 0
Share

Thanks Julien, that's great to know. :-)

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

13 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

Related Questions

Problem with Unity Ray Trace AI Detect Player 0 Answers

Another Raycast Issue. 0 Answers

Have falling object exit from a collider after collision? 2 Answers

Can you figure out raycast origin position from RacyastHit? 2 Answers

Physics.Raycast not working as expected 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