- Home /
How to instantiate an object by touching
I would like to know how to instantiate an object by touching a screen on a mobile device.
Answer by IEMatrixGuy · Nov 14, 2014 at 03:32 AM
Hi,
You can simply use the Instantiate command, on a mouseDown event (Input.GetMouseDown(0)) since tapping is the mobile equivalent of left click. Then simply instantiate the desired object at the mouse position, if that's what you want, which can be done with .
Camera.main.ScreenPointToRay(Input.mousePosition);
Alternatively you could also use a ray, to check for collisions, see:
http://answers.unity3d.com/questions/491711/getting-the-position-of-the-mouse-in-2d-space.html
Hope that helped :)
IEMatrixGuy
Heres what I have
void Update() { if (Input.Get$$anonymous$$ouseButtonDown(0) ("Fire1") && Time.time > nextFire) { nextFire = Time.time + fireRate; Instantiate (shot, shotSpawn.position,shotSpawn.rotation); } }
But heres what Unity says
Assets/PlayerController.cs(22,32): error CS0119: Expression denotes a value', where a
method group' was expected
Your answer
Follow this Question
Related Questions
Box colliders getting Stuck 1 Answer
Touch input not working 3 Answers
Get position of touch 2 Answers
Code generating 2 different results? 1 Answer
How to change cameras by a single touch on my phone's screen? (Android) 1 Answer