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 Dawnk41 · Apr 23, 2014 at 02:21 AM · raycastvector3physics.raycast

Problem with converting RaycastHit to Vector3

Hi, when I used this code: using UnityEngine; using System.Collections;

 public class Mousetargeting : MonoBehaviour
 {
     void Update ()
     {
         if(Input.GetMouseButtonDown(1))
         {
             RaycastHit mouseHit;
 
             if(Physics.Raycast (Camera.main.transform.position,Input.mousePosition, out mouseHit))
             {
                 transform.position = mouseHit.point;
             }
         }
     }
 }

I get the error, "error CS0029: Cannot implicitly convert type 'UnityEngine.RaycastHit' to 'UnityEngine.Vector3'

I'm trying to move the object that this code is in to the point where the mouse is whenever I right click on the screen. Why can't RaycastHit be recorded as a Vector3?

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 anmonta · Apr 23, 2014 at 08:44 AM

There is a coordinate system for camera, world and terrain. In essence your missing the conversion. The example below goes a step further and shows one way to use a layer mask for filtering out specific layers, but you can comment that out. I simplified this from what I am using for a better example.

         public static Vector3 GetRealCords()
         {
             /// <summary>
             /// Translate current Mouse X/Y into realworld cords using raycast
             /// </summary>
             /// <returns>Vector3 realworld cords (0,0,0) for invalid queries</returns>
             /// 
 
             Ray xray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
             RaycastHit hit;
 
             int mask = 0;
             mask |= (1 << LayerMask.NameToLayer("Building"));
             mask |= (1 << LayerMask.NameToLayer("Trees"));
             mask |= (1 << LayerMask.NameToLayer("nodes"));
             mask |= (1 << LayerMask.NameToLayer("Ignore Raycast"));
             mask = ~mask;
 
 
             if (Physics.Raycast(xray, out hit, 1000, mask))
             {
                 if (hit.collider.gameObject.name == "Terrain")
                 {
 
                     return hit.point;
                 }
 
             }
 
             return Vector3.zero;
         }
Comment
Add comment · 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
0

Answer by Simon-Larsen · Apr 23, 2014 at 02:25 AM

If you want to extract the "impact point in world space where the ray hit the collider", in other words, the vector3 you expect from your raycast hit. Then you need to get the "point" variable of the RaycastHit class

 RaycastHit mouseHit;
 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

 if (Physics.Raycast(ray, out mouseHit, 100)) 
 {
     transform.position = mouseHit.point;
 }

For more info about raycasting, read the Unity Script Reference at https://docs.unity3d.com/Documentation/ScriptReference/RaycastHit.html

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 Dawnk41 · Apr 23, 2014 at 02:47 AM 0
Share

Hmm, alright, I've made that change to the code, (And a few others), and now, the code sorta works... but ins$$anonymous$$d of the object being moved to where I right click, it moves to point (0, -0.45, 0). I can't figure out what is causing this. Here is the new code:

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$ousetargeting : $$anonymous$$onoBehaviour
 {
     void Update ()
     {
         if(Input.Get$$anonymous$$ouseButtonDown(1))
         {
             RaycastHit mouseHit;
 
             Physics.Raycast (Camera.main.transform.position,Input.mousePosition, out mouseHit);
             transform.position = new Vector3(mouseHit.point.x, -0.45f, mouseHit.point.z);
         }
     }
 }

Any idea what is causing it?

avatar image Simon-Larsen · Apr 23, 2014 at 12:58 PM 0
Share

Editted the code, but follow anmonta answer if you want to go a little more advanced and differentiate between layers.

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

22 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

Related Questions

How to get an offset of a RaycastHit? 0 Answers

Get the position of the first finger that touched the collider? 1 Answer

Why multiply a vector by -2? 1 Answer

Slant Physics Raycast Implementation 0 Answers

How to Spherecast/Raycast diagonally? 1 Answer


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