- Home /
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?
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;
}
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
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?
Editted the code, but follow anmonta answer if you want to go a little more advanced and differentiate between layers.
Your answer
Follow this Question
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