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 Coolnumber11 · Apr 16, 2015 at 09:33 PM · instantiateraycast

Problem with Instantiating object at Raycast hit point.

Hey everyone, I'm having problems with raycast. I'm trying to instantiate a gameobject at the position where the raycast hits. The raycast always sends from the centre of the screen. Whenever I test this out without maximizing the screen, it works perfectly but when maximized, the object instantiates at only a small distance from the player. I've narrowed it down to the code where I find the centre of the screen, but I'm unsre how this is having an effect on the raycast distance. Here's my code.

     if(Input.GetMouseButtonDown(1)){
         Ray ray = Camera.main.ScreenPointToRay(new Vector3((Screen.height * 0.5f), (Screen.width * 0.5f), 0);
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit, 5000)){
             object = Instantiate(object, hit.point, Quaternion.identity) as GameObject;
         }

Thanks for your help!

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 Owen-Reynolds · Apr 16, 2015 at 11:52 PM 0
Share

Try printing hit.transform.name? $$anonymous$$aybe a change in aspect ratio is moving something in front of you.

Why do you think it's the "center of screen" code? But, you can check by printing ray.start and ray.direction. Compare start to the camera pos in the game world.

avatar image Flickayy · Apr 17, 2015 at 12:16 AM 0
Share

Am I just going crazy, but everytime I've seen someone find the centre of the screen, they've divided by 2 not 0.5f?

avatar image KdRWaylander · Apr 17, 2015 at 06:09 AM 1
Share

Well, dividing by 0.5f ... would multiply by 2, ins$$anonymous$$d of dividing ?

avatar image jessee03 · Apr 17, 2015 at 06:19 AM 0
Share

Shouldn't you be using Screen.height/2 and Screen.width/2

avatar image AlwaysSunny · Apr 17, 2015 at 07:25 AM 3
Share

Just use a ray whose direction is camera.forward.
The ray's origin is the camera.position.
Eli$$anonymous$$ate this unnecessary step altogether.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

9 People are following this question.

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

Related Questions

Make an instantiated object match the slope of the terrain 1 Answer

Raycast Specific Object/Instantiate Explosion 1 Answer

Instantiate gameobject a certain distance away from transform 2 Answers

How do I instantiate a projectile along the path of a raycast? 0 Answers

Instantiate prefab from original position to click mouse point position 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