// Shoot a ray whenever the user taps on the screen
var particle : GameObject;
function Update () {
for (var i = 0; i < Input.touchCount; ++i) {
if (Input.GetTouch(i).phase == TouchPhase.Began) {
// Construct a ray from the current touch coordinates
var ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position);
if (Physics.Raycast (ray)) {
// Create a particle if hit
Instantiate (particle, transform.position, transform.rotation);
}
}
}
}
Can anyone help me with the above code? I copied it directly from http://unity3d.com/support/documentation/ScriptReference/Input.GetTouch.html to use in my game.
I got the error: “Assets/Scripts/_pistolAI.js(7,47): BCE0019: ‘GetTouch’ is not a member of ‘UnityEngine.Input’. Did you mean ‘GetKey’ ?”
SOmeone said it’s for Unity 3.1… The documentation did not specify… if so what would it look like for iPhone 1.7? Just trying to get a simple FPS going using rays for bullets.
Unity iPhone 1.7 o less do not support if GetTouch or TouchPhase but iPhoneInput.GetTouch o like , check documentation for unity iphone 1.7 .
GetTouch or TouchPhase is for unity 3.x
Try this but it is untested .
var particle : GameObject ;
function Update ()
{
for (var i = 0; i < iPhoneInput.touchCount; ++i) {
if (iPhoneInput.GetTouch(i).phase == iPhoneTouchPhase.Began)
// Construct a ray from the current touch coordinates
var ray = Camera.main.ScreenPointToRay (iPhoneInput.GetTouch(i).position);
if (Physics.Raycast (ray)) {
// Create a particle if hit
Instantiate (particle, transform.position, transform.rotation);
}
}
}
Here is what I am trying to do: Tap anywhere on the iPhone screen to fire a projectile at a target. Picture a shooting gallery. The code below is not working correctly. I am using a gameObject so I can see what’s going on for the bullets. All I get is a spread of the objects just spawning and falling…
var particle : GameObject;
var reticle : Texture2D;
function OnGUI() {
var pos = iPhoneInput.GetTouch(0).position;
var texRect = new Rect(pos.x - reticle.width / 2, +pos.y - reticle.height / 2, reticle.width, reticle.height);
GUI.DrawTexture(texRect, reticle);
for (var i = 0; i < iPhoneInput.touchCount; ++i) {
if (iPhoneInput.GetTouch(i).phase == iPhoneTouchPhase.Began)
// Construct a ray from the current touch coordinates
var ray = Camera.main.ScreenPointToRay (iPhoneInput.GetTouch(i).position);
if (Physics.Raycast (ray)) {
// Create a particle if hit
Instantiate (particle, transform.position, transform.rotation);
}
}
}
you should never use gettouch anyway. if you iterate through the whole thing use .touches and get the touch object directly