- Home /
iOS touch triggers the wrong object
Hello. Can someone tell me what I'm doing wrong? I have several objects in a scene, and I want to be able to tap them to to manipulate them in different ways. Some of the objects are fine, I tap them and they animate/whatever. But when I tap some other objects, an animation in a completely different object occurs. I feel like I'm missing something simple here and I think I've come across this question here before but I can't find it now.
Please help me :)
EDIT: this is the code in the js-script attached to the object I tap and want to animate:
function Update () {
}
function FixedUpdate() {
for(var touch : Touch in Input.touches)
{
var ray : Ray = Camera.main.ScreenPointToRay(touch.position);
if(touch.phase == TouchPhase.Ended)
{
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100.0)) {
//Debug.Log("Touched...");
//var curX = Input.GetTouch(0).position.x;
//var curY = Input.GetTouch(0).position.y;
//Pos = Instantiate(curX, curY, 1000);
//Pos.rigidbody.AddForce(transform.forward * speed);
rigidbody.AddForce(0, 1200, 0);
}
}
}
}
And this is the code in the cs-script attached to the object playing its animation:
public class TouchBlueTeapot : MonoBehaviour {
public Animation m_animation;
// Use this for initialization
void Start () {
Debug.Log("started");
}
// Update is called once per frame
void Update () {
foreach (Touch touch in Input.touches)
{
Ray ray = Camera.main.ScreenPointToRay(touch.position);
Debug.Log("RAY:" + ray.direction);
if (touch.phase == TouchPhase.Began)
{
Debug.Log("touchphase position:" + touch.position);
RaycastHit hit = new RaycastHit();
if (Physics.Raycast(ray, out hit, 1000))
{
Debug.Log("touched " + hit.transform.name);
gameObject.animation.Play();
}
}
}
}
}
Sorry if it looks kind of crappy...
Could you please provide the code you are currently using? Also, just as a precaution (because it seems to happen a lot), could you make sure that when you do copy in your code, highlight the entire code, and press the 101010 button in the toolbar to format it correctly. :)
Sure! Should I provide the code in the script attached to the object or also the script attached to the object being animated?