Just trying to learn the basics of scripting for unity iphone. Took scripting reference entry to iPhoneInput.GetTouch and wrote:
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);
Debug.Log (ray);
if (Physics.Raycast(ray)) {
// Create a particle if hit
Instantiate(particle, transform.position, transform.rotation);
}
}
}
}
But, whilst every tap on the iphone screen -does- generate an instance of the prefab, it always does so in the middle of the screen, not where I tapped. The log shows what look like appropriate Vector2 values:
Thanks so much! Now, if I could only figure out -how- :lol:
I’m assuming that transform.position, transform.rotation are really a Vector2 (2d screen value), and that what you mean is that this needs to be converted into a ‘real world’, 3D value, i.e. a Vector3?
But in my code, I’m passing variable ‘ray’, where I store the value of iPhoneInput.GetTouch(i).position.
So, I can see that the gizmos function returns a vector 3 value and then uses that to draw a yellow circle, but how do I pass that vector 3 value to position the creation my prefab?
transform.position is just wherever the object is. You’re telling whatever you’re instantiating to show up wherever the object is. That’s not what you want (unless you first move the object to the position of the touch).
You’re not passing it. You’re just checking to see if it hits any collider. I’m actually going to assume you can use ray.origin, however, for the second parameter. I’m not sure exactly where the origin is, but my intuition is right on the near clip plane. It might work for a 2D object. I’d probably push it a little farther into the camera’s frustum, using that function I linked to, instead. Things that are right on the near clip plane tend to disappear or get partially clipped, in my experience. If this is a mesh you’re instantiating, you can offset its pivot point too, if that’s easier.
I didn’t suggest using the example in the docs; that’s close to useless for your purposes.
Not true. You don’t quite understand what’s going on yet but I’m sure you can get a better grasp on it. 8)