Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by gg_ez · Oct 28, 2017 at 06:41 PM · raycastpositionobjectraycasthitray

Raycast is offsetting object

I'm shooting a simple Raycast out of the main camera. A object will then be instantiated that will follow the ray. However the object is getting offset off of the ray, which I don't want. Any idea why this is happening?

This is the Ray:

Ray ray = Camera.main.ViewportPointToRay(new Vector2(0.5f, 0.5f)); RaycastHit hit;

And this is the object's position:

 transform.position = hit.point;

Here is a gif: link text

If anyone knows the solution or has some knowledge about this offset-thing, I would really appreciate an answer.

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 unidad2pete · Oct 28, 2017 at 06:59 PM 0
Share

Im not sure, but seems like your Gizmos drawline is the incorrect thing not your cube. If you see the game Window, the right window, you are making a ray to the center on this windows, and the position seems to be fine (I cant see with precision)

If you share all code you are using, we can help you better, because I think the problem is on other way of your code. In this case I need to $$anonymous$$now How are drawing your red line

1 Reply

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

Answer by unidad2pete · Oct 28, 2017 at 08:46 PM

I dont understand what do you mean when you say " but when I move with the object in my hand, it moves the object further away when I move with it"

but referencing to the red line, I supose your script is the town, I dont know, but I try to explain me better.

              Debug.DrawLine(transform.position, transform.forward * range, Color.red, 0.01f);
 

If the script is on the Town, the line direction its based on the forward of the Town but here:

 Camera.main.ViewportPointToRay(new Vector2(0.5f, 0.5f));

the ray starts on the center of the game window to the center of the game window, then you are using 2 diferent rays.

 // this is the ray of side off the town
         RaycastHit hit;
         if(Physics.Linecast(transform.position, transform.forward, out hit))
         {
             yourCube.transform.position = hit.point; 
         }

         // this is the ray of your camera
         Ray ray = Camera.main.ViewportPointToRay(new Vector2(0.5f, 0.5f));
 
         if (Physics.Raycast(ray, out hit, range))
         {
             yourCube.transform.position = hit.point;
         }
 
         // You can see the diference drawing both lines
 
         Debug.DrawLine(transform.position, transform.forward * range, Color.red, 0.01f);
         Debug.DrawRay(ray.origin, ray.direction, Color.green, 0.01f);


Try both and test the diference.

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 unidad2pete · Oct 28, 2017 at 08:54 PM 0
Share

The part of the object moevent, if you mean when the grid is actived the movement is diferent, is because the grid is based on position with any distance, I mean, you have a grid with 4*4 , then position on first grid is (0,0,0) , grid 2 (2,0,0) grid 3 (0,0,2) grid 4 (2,0,2), then the object teleport with distance of 2. When you moves the object in your hand, the object can move with precise positions, (0,0,0) , (0.1f, 0, 0) , (0.2f, 0,0) etc..

Do you mean this?

And be careful with the example of my answer Im using the same Raycasthit for both rays, you should make 2 rays to see the diference on same moment.

 RaycastHit hit1; // hit for town ray
 RaycastHit hit2; //hit for your camera

avatar image gg_ez · Oct 29, 2017 at 01:40 PM 0
Share

Thank you. The Raycasting works perfectly now. The object is exactly in the middle of the hitpoint of the ray now, just like how I wanted it to be.

And I also got the grid system working, because I had accidentally commented out the wrong line. So that works now too.

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

114 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

Related Questions

RayCast(Ray ray, out RaycastHit hitInfo, float maxDistance, int layerMask) question 0 Answers

Physics.Raycast Not Working... Kinda 0 Answers

Why am I getting a Null Reference Exception? 0 Answers

The casted ray is persisting. 1 Answer

Do touch on point where ray hits 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