• 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 tuf91497 · Jul 17, 2019 at 04:48 PM · scripting problemraycastraycastingraycasthitraycasts

Can you figure out raycast origin position from RacyastHit?

I've got a world-space menu in virtual reality that works with raycasting (in my app you raycast from your fingertip). Whenever I hit an input field with my fingertip raycast, I want my keyboard (which is its own gameObject), to pop up in between the origin of the raycast and the menu, facing the player.

I have this (mostly) working by just lerping between Camera.main.transform.position and raycastHit.point, but I'd really like it to be between the fingertip which raycasted and the menu which was the raycastee.

In a raycastHit object, you get racycastHit.point and raycastHit.distance for free. You don't get the origin point of the raycast, nor, as far as I can see, do you get the actual direction of the raycast. You get the normal of the plane which you hit, which is not enough considering the menu could be at any angle when you hit it with a raycast.

tldr, is there a way to extrapolate the Vector3 origin of your raycast from raycast hit info?

Comment
Add comment
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

2 Replies

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

Answer by arichards · Jul 17, 2019 at 04:57 PM

Usually you'll have the raycast (and the position passed into the raycast) still in scope when you call the Raycast method. Is this not the case? Maybe there's some way to capture those values?

Edit: See comment below. Editing to add potential solution:

Can your Raycast UnityEvent pass an instance of a class that contains both a Raycast and a RaycastHit? Or you can use a .NET event to pass more than one parameter. The code would be:

 // RaycastTrigger.cs
 public event Action<Raycast, RaycastHit> RaycastHitOccurred = delegate {};

 // Invoke the event:
 RaycastHitOccurred(myRaycast, myRaycastHit);
 
 // Consumer.cs
 private void Start()
 {
     raycastTriggerInstance.RaycastHitOccurred += HandleRaycastHitOccurred;
 }
 
 private void HandleRaycastHitOccurred(Raycast raycast, RaycastHit raycastHit)
 {
     // Do stuff
 }
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 tuf91497 · Jul 17, 2019 at 05:49 PM 0
Share

Not in my case. When my raycasting script hits a viable object, it looks for a "RaycastTrigger" component which invokes a UnityEvent, which obviously only passes the RaycastHit onwards to whatever script wants it.

I think it may be enough to just spawn the keyboard a little bit below the camera

avatar image arichards · Jul 17, 2019 at 06:08 PM 0
Share

Edited answer to reflect the contents of this comment.

avatar image
0

Answer by tuf91497 · Jul 22, 2019 at 04:14 PM

A simpler answer for my solution has been to add an invisible anchor game object onto my menu keyboard at the point that I want it to spawn, and then to just move my keyboard to that position

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 arichards · Jul 22, 2019 at 06:21 PM 1
Share

A totally valid approach! I use empty transforms for that all the time.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

230 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Using Ray cast for click to move 2 Answers

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

Raycast is constrained to the horizontal axis 1 Answer

[help]RayCast check [SOLVED] 2 Answers

Raycast returns null for no apparent reason 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges