Reputation: 167
When I click right click I want to create new sphere. And I don't know why this don't work. It creates a sphere, but definitely not on mouse position!
Vector2 mousePos;
public Transform mousePointer;
float mouseX, mouseY;
Vector3 spawnPoint;
void Start () {
}
void Update () {
if(Input.GetMouseButtonDown(1)){
mousePos = Input.mousePosition;
mouseX = Input.mousePosition.x;
mouseY = Input.mousePosition.y;
spawnPoint = new Vector3(mouseX, mouseY, 0);
Instantiate(mousePointer, spawnPoint, Quaternion.identity);
}
}
Upvotes: 2
Views: 686
Reputation: 5515
Try spawning the object relative to the camera.
For example, use spawnPoint = cameraPosition + new Vector3(mouseX, mouseY, 0);
or something similar. Check out the related post: Create a cube relative to camera mouse position.
The object is being spawned in global coordinates.
Upvotes: 1