- Home /
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.
[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);
}
}
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.
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.
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
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)
Your answer
Follow this Question
Related Questions
[help]RayCast check [SOLVED] 2 Answers
Raycast not working 2 Answers
Can you figure out raycast origin position from RacyastHit? 2 Answers
My raycast is ignoring my tilemap collider 0 Answers
Raycast detects box collider, but not capsule collider 0 Answers