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 Shawn Miller · May 08, 2012 at 12:22 AM · raycastscreenpointtoray

ScreenPointToRay gives unexpected results?

The results that I'm getting from this script are not what they should be. I'm attempting to get an object to fire from my player to a point in the world which is at the center of the screen, but it always seems to be too low or just off the wall. The further the hit, the more accurate it is, and conversely, the closer, the farther away from the mark it seems to go (though this might be a perspective thing).

p.Launch() is just a function which lerps from the object's current position to the position given.

With the uncommented version of p.Launch, I get the most accurate results, the situation described above. This shouldn't really be the case since ray.direction is just a direction vector multiplied by a scalar and shouldn't give even semi-accurate results unless the player is standing near (0,0,0) in world space.

The commented version is, by far, worse. Technically it works and it's supposed to give the most accurate results since it's an actual position vector, but only if you're aiming at the exact point that it wants, otherwise it shoots them off in all sorts of odd directions, getting more random the less you are facing that point.

 void Update () {
     if(Input.GetMouseButtonDown(0)) {
         GameObject newBullet = Instantiate(bullet, transform.position, transform.rotation) as GameObject;
         Projectile p = newBullet.GetComponent(typeof(Projectile)) as Projectile;
         
         Vector3 screenPoint = new Vector3(Screen.width/2.0f, Screen.height/2.0f);
         Ray ray = Camera.main.ScreenPointToRay(screenPoint);
         
         RaycastHit hit;
         Physics.Raycast(ray, out hit);

         // Works but only for a very small section of the world, otherwise, results are very sporadic.
         //p.Launch(hit.point);

         // Moves in the general direction it should, but always too low.
         p.Launch (ray.direction * hit.distance);
     }
 }
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 Shawn Miller · May 08, 2012 at 12:25 AM 0
Share

I would also like to add that I have verified that I am using the center point of the screen and have even placed a target reticle on the GUI over it and verified its centeredness. The Raycast also seems to ignore most colliders for reasons I can't explain.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by pudd1nG · May 08, 2012 at 12:52 AM

 Vector3 screenPoint = new Vector3(Screen.width/2.0f, Screen.height/2.0f);

You're missing an axis, not sure if the default will work for you.

EDIT: you've got a background object for the raycast to hit, right? You will get strange results should the raycast go on forever (your not limiting it either)

Comment
Add comment · Show 3 · 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 Shawn Miller · May 08, 2012 at 01:17 AM 0
Share

It defaults to 0 but it doesn't matter because ScreenPointToRay ignores the z-axis anyway.

http://unity3d.com/support/documentation/ScriptReference/Camera.ScreenPointToRay.html

avatar image pudd1nG · May 08, 2012 at 01:48 AM 0
Share

See my edit but I'm thinking your issue is with the hit.point. It would also be a good idea to put a check to make sure the raycast actually hit something before continuing, this will catch odd raycast behaviors in most cases too

avatar image Shawn Miller · May 08, 2012 at 03:07 AM 0
Share

It is always hitting something, and I even just added the check just in case but I've never gotten a NullReferenceException so I knew that wasn't the problem.

avatar image
0

Answer by Shawn Miller · May 08, 2012 at 06:22 AM

Every time...

Here goes. Turns out, I originally had intended Projectile.Launch() to take a distance vector and add it to the transform of the object, which explains why the uncommented version works better (somewhere between then and when I wrote the above code I forgot about that). The problem with that is that it was spawning at the player, rather than the camera so it was taking the position of the player instead of the camera.

After taking out that bit of code from the Launch function, all I had to do was add the camera's position to the direction vector:

 p.Launch((ray.direction * hit.distance) + Camera.main.transform.position);

And now it works.

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 pudd1nG · May 08, 2012 at 06:28 AM 0
Share

Ahh, I was assu$$anonymous$$g your Launch function took care of that XD

Glad to hear you've got it working, happy unity-ing!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast not calculating correctly (doesn't go to mouse position) 2 Answers

Android Unable to track finger touch after orientation change 0 Answers

ScreenToRayPoint not working in VR, how to substitute? 0 Answers

Is there a limit on the distance of raycast? 2 Answers

Get 2D Collider with 3D Ray 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