- Home /
Using Input.mouseposition to instantiate
NOTE: the y val is -100 regardless, the problem is with the x and z values.
I'm using input.mouseposition for the vector 3 in instantiate:
GameObject.Instantiate (object, Input.mousePosition, transform.rotation);
when I click on the screen the object appears around (400, y, 0) but where I'm clicking is around (-18, y, 25). any ideas whats going on? I tried to correct the differences with this but it only worked in the positions I stated above.
private int YPos = -100;
private float XPos = -0.04215457f;
void Start () {
transform.position = new Vector3(transform.position.x * XPos, YPos, (transform.position.z + 0.1f) * 250 );
}
Answer by Jamora · Jun 22, 2013 at 09:24 AM
If your mousePosition.y is negative, it means you're clicking below your screen. Mouseposition is also always 2-dimensional; z is 0. If you want to instantiate in your game world, you could try Camera.main.ScreenToWorldPoint()
. You will need to provide a Vector3, where the z is the distance from the camera (e.g. via a raycast) and x,y come from mousePosition.
Your answer
Follow this Question
Related Questions
if function broken? 3 Answers
Checking if the Mouse Has a Middle Button 1 Answer
C# inputString mouse Keys? 1 Answer
mouse Input Instantiate 0 Answers
Distribute terrain in zones 3 Answers