- Home /
When I touch one object all others react
I have issue with touch detection. I placed some cubes and atteched this script for everyone. When I touch one cube it reacts for all cubes and I don't know why. I using this code for touch detection.
private bool button = false;
public Transform target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
RaycastHit hit = new RaycastHit();
for (int i = 0; i < Input.touchCount; ++i) {
if (Input.GetTouch(i).phase.Equals(TouchPhase.Began)) {
// Construct a ray from the current touch coordinates
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
if (Physics.Raycast(ray, out hit)) {
if (button) {
target.position += new Vector3 (0, 0, 30);
button = false;
} else {
target.position += new Vector3 (0, 0, -30);
button = true;
}
}
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Android touch 3d Object event 1 Answer
Make object block a raycast 1 Answer
Ignore Touch 2 Answers
Joystick Raycast Problem? 2 Answers