Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 cankirici34 · Aug 05, 2021 at 03:29 PM · raycastraycastingraycasthitraycastallraycasts

ScreenPointToRay ray not coming through from camera

I just want Ray to come out of the middle of the camera but instead comes from somewhere else as seen in the editor. You can also see in the screenshot. is this normal? And again, as you can see in the screenshot, when I instantieate an object, that object should be in the hit info of the ray, but there is too much distance between them (between the mouse and the object). I think I'm making a logic error or something I don't know. alt text

 [SerializeField] private Camera warehouseCam;
     private RaycastHit _hitInfo;
     private Ray ray;
 
     [SerializeField] private GameObject _prefabTransParent;
     [SerializeField] private GameObject _prefab;
     private GameObject go = null;
 
     public GameObject terrain;
     // Update is called once per frame
     void Update()
     {
         ray = warehouseCam.ScreenPointToRay(Input.mousePosition);
 
 
         if (Physics.Raycast(ray, out _hitInfo, Mathf.Infinity))
         {
             Debug.DrawLine(_hitInfo.point, terrain.transform.position, Color.red);
 
             if (_hitInfo.transform.tag == "warehouse")
             {
                 if (Input.GetKeyDown(KeyCode.E))
                 {
                     go = Instantiate(_prefabTransParent, _hitInfo.point, Quaternion.identity);
                 }
                 if (go != null)
                 {
                     go.transform.position = _hitInfo.point;
                 }
             }
         }
 
         if (Input.GetMouseButtonDown(1)) // Cancel Placing
         {
             Destroy(go);
             
         }
         else if (Input.GetMouseButtonDown(0) && go != null)
         {
             Instantiate(_prefab, go.transform.position, go.transform.rotation);
             Destroy(go);
             
         }
 
     }


ekran-alıntısı.png (502.5 kB)
Comment
Add comment · Show 7
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 CodesCove · Aug 05, 2021 at 10:32 PM 1
Share

if you want your Debug.DrawLine to present the ray then it's not doing that because you set it from the hit point to the terrain transforms position (probably 0,0,0?). Instead you should use warehouseCam.transform.position.

The _prefabTransParent should instantiate just fine where the ray hits. Are you sure your mouse position is how you drew it?

Side note: I don't quite understand why you cast ray in every frame and not only after player presses $$anonymous$$ It's quite inefficient now.

avatar image cankirici34 CodesCove · Aug 06, 2021 at 07:03 AM 0
Share

Hi, yes you were right, I wrote the Drawline part wrong, I fixed it. Debug.DrawRay(warehouseCam.transform.position, terrain.transform.position, Color.red); I tried this and this: Debug.DrawLine(warehouseCam.transform.position, terrain.transform.position, Color.red); but this time the gizmos didn't move even though it came from the camera, interesting. and yes I'm sure my mouse position is where I draw.

avatar image cankirici34 CodesCove · Aug 06, 2021 at 09:23 AM 0
Share

hey can you watch the video i showed my problem https://www.youtube.com/watch?v=Cv5exUmNaoc&ab_channel=%C3%87a%C4%9Flar and here is my code https://codeshare.io/OdN1xx

avatar image CodesCove cankirici34 · Aug 06, 2021 at 10:44 AM 1
Share

The Debug should be Debug.DrawLine(warehouseCam.transform.position, _hitInfo.point, Color.red); as it is now in your code

NOTICE: I'm now commenting to you code behind the link you sent, not the code you posted (there is difference in there)

Looking at the other code (code in you link, not the one you posted) the only problem I see is when you are placing the _prefabTransParent (after pressing E) and if then _prefabTransParent has a Collider then the Ray will hit that Collider instead of the Terrain Collider and get wrong position. So change that logic + also add one null check.

Something like this (as it was in your posted code):

 if (_hitInfo.transform.tag == "warehouse")
             {
                 if (Input.GetKeyDown(KeyCode.E) && go == null)
                 {
                     go = Instantiate(_prefabTransParent, _hitInfo.point, Quaternion.identity);
                 }
 
                 if (go != null)
                 {
                     go.transform.position = _hitInfo.point;
                 }
             }   

Otherwise I don's see any problem

PS: You could use physics layer check in raycast which would be much more efficient and you don't get any wrong hits + you don't need check the tags)

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

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

[help]RayCast check [SOLVED] 2 Answers

Play Animation on Raycast *open and close some objects* 0 Answers

Raycast is constrained to the horizontal axis 1 Answer

Raycast returns null for no apparent reason 0 Answers

Raycast not working 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