Question by
iLucas13 · Jul 31, 2017 at 05:14 AM ·
c#ios2d gametouch controls
My object disappears when being dragged through the screen
I am currently testing my game on an iPhone and it consists simply of a sprite that gets transformed to the position of the touch. It works fine on the bottom half of the screen, but it simply disappears when I try to drag it to the top half. Any help is appreciated!
This is the script that manages the touching:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchLogic : MonoBehaviour {
public static int currTouch = 0;
private Ray ray;
private RaycastHit rayHitInfo = new RaycastHit ();
[HideInInspector]
public int touch2Watch = 64;
// Update is called once per frame
void Update ()
{
if (Input.touches.Length <= 0)
{
} else {
for (int i = 0; i < Input.touchCount; i++)
{
currTouch = i;
if (this.GetComponent <GUITexture >() != null && (this.GetComponent <GUITexture >().HitTest (Input.GetTouch (i).position)))
{
if (Input.GetTouch (i).phase == TouchPhase.Began)
{
this.SendMessage ("OnTouchBegan");
}
if (Input.GetTouch (i).phase == TouchPhase.Ended )
{
this.SendMessage ("OnTouchEnded");
}
if (Input.GetTouch (i).phase == TouchPhase.Moved )
{
this.SendMessage ("OnTouchMoved");
}
}
ray = Camera.main.ScreenPointToRay (Input.GetTouch (i).position);
switch (Input.GetTouch (i).phase)
{
case TouchPhase .Began:
this.SendMessage ("OnTouchBeganAnywhere");
if (Physics.Raycast (ray, out rayHitInfo))
rayHitInfo.transform.gameObject.SendMessage ("OnTouchBegan2D");
break;
case TouchPhase .Ended :
this.SendMessage ("OnTouchEndedAnywhere");
if (Physics.Raycast (ray, out rayHitInfo))
rayHitInfo.transform.gameObject.SendMessage ("OnTouchEnded2D");
break;
case TouchPhase .Moved :
this.SendMessage ("OnTouchMovedAnywhere");
if (Physics.Raycast (ray, out rayHitInfo))
rayHitInfo.transform.gameObject.SendMessage ("OnTouchMoved2D");
break;
case TouchPhase .Stationary :
this.SendMessage ("OnTouchStayedAnywhere");
if (Physics.Raycast (ray, out rayHitInfo))
rayHitInfo.transform.gameObject.SendMessage ("OnTouchStayed2D");
break;
}
}
}
}
}
The script that controls the behavior of the sprite:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchLogic : MonoBehaviour {
public static int currTouch = 0;
private Ray ray;
private RaycastHit rayHitInfo = new RaycastHit ();
[HideInInspector]
public int touch2Watch = 64;
// Update is called once per frame
void Update ()
{
if (Input.touches.Length <= 0)
{
} else {
for (int i = 0; i < Input.touchCount; i++)
{
currTouch = i;
if (this.GetComponent <GUITexture >() != null && (this.GetComponent <GUITexture >().HitTest (Input.GetTouch (i).position)))
{
if (Input.GetTouch (i).phase == TouchPhase.Began)
{
this.SendMessage ("OnTouchBegan");
}
if (Input.GetTouch (i).phase == TouchPhase.Ended )
{
this.SendMessage ("OnTouchEnded");
}
if (Input.GetTouch (i).phase == TouchPhase.Moved )
{
this.SendMessage ("OnTouchMoved");
}
}
ray = Camera.main.ScreenPointToRay (Input.GetTouch (i).position);
switch (Input.GetTouch (i).phase)
{
case TouchPhase .Began:
this.SendMessage ("OnTouchBeganAnywhere");
if (Physics.Raycast (ray, out rayHitInfo))
rayHitInfo.transform.gameObject.SendMessage ("OnTouchBegan2D");
break;
case TouchPhase .Ended :
this.SendMessage ("OnTouchEndedAnywhere");
if (Physics.Raycast (ray, out rayHitInfo))
rayHitInfo.transform.gameObject.SendMessage ("OnTouchEnded2D");
break;
case TouchPhase .Moved :
this.SendMessage ("OnTouchMovedAnywhere");
if (Physics.Raycast (ray, out rayHitInfo))
rayHitInfo.transform.gameObject.SendMessage ("OnTouchMoved2D");
break;
case TouchPhase .Stationary :
this.SendMessage ("OnTouchStayedAnywhere");
if (Physics.Raycast (ray, out rayHitInfo))
rayHitInfo.transform.gameObject.SendMessage ("OnTouchStayed2D");
break;
}
}
}
}
}
Comment