Question by
FyreDemon405 · May 01, 2017 at 02:47 PM ·
raycastmousepositionintantiate
Instatiated object appears at world center instead of mouse position
I am having the problem of a Prefab spawning at the center of the world when i try to instatiate it at the mouse position.
using UnityEngine;
public class Enemy_spawner : MonoBehaviour {
public Transform soldier;
void Update()
{
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray,out hit))
{
Instantiate(soldier,hit.point,Quaternion.identity);
}
}
}
}
Any help would be appreaciated thank you.
Comment
The script works fine, in my test project. Do you have multiple cameras in the scene? In that case can you make sure it is getting the right camera to raycast?
Found the problem: I had an animation that was only moving it on the y axis so I have no idea why it would change it but it did
Your answer
![](https://koobas.hobune.stream/wayback/20220612121042im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
assigning several positions and call array of raycasts 0 Answers
My Ray is going the wrong way 1 Answer
Raycast not drawing where i tell it 1 Answer