- Home /
Touch and Drag Issues Unity 2D Game
I am having a few issues with my touch controls.
1) My game is 2D. When I tested this on my android device the 2D image will almost disappear when shifting from right touch to left touch. The object is being treated as 3D object. I think this has something to do with the Z space. Here's a link to a video I made showcasing the problem (video description might elaborate more on my desired results) - https://www.youtube.com/watch?v=m0EgEqereys
2) How could I make the character move like in this video - https://www.youtube.com/watch?v=5hckY75j_lg. In this case any place you touch the screen grabs the player (head) and moves exactly exactly as the finger does.
Any help would appreciated!
Here's my code:
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;
}
}
}
}
}
public class FollowTouch : TouchLogic {
public float speed=1f;
private Vector3 finger;
private Transform myTrans, camTrans;
void Start () {
myTrans = this.transform;
camTrans = Camera.main.transform;
}
void LookAtFinger(){
Vector3 tempTouch=new Vector3 (Input.GetTouch (touch2Watch ).position .x,Input.GetTouch (touch2Watch ).position .y,
camTrans .position.y-myTrans .position .y);
finger = Camera.main.ScreenToWorldPoint (tempTouch);
myTrans.LookAt (finger);
myTrans.Translate (Vector3.forward * speed * Time.deltaTime);
}
void OnTouchMovedAnywhere(){
LookAtFinger ();
}
void OnTouchStayedAnywhere(){
LookAtFinger ();
}
void OnTouchBeganAnywhere(){
touch2Watch = TouchLogic.currTouch;
}
}
I have spent several days on this issue and still no luck! This is driving me crazy! I even tried incorporating some new code and mixing and matching code snippets. I 2 other code bits I have tried, but cannot get the character(sprite) to look at the touch position. I tried using the lookat function, but no luck. What could I add to the code (or the one above) to get the desired results?
Alternate touch control code (1):
public class TouchDrag : $$anonymous$$onoBehaviour {
public float speed = 0.1f;
void Update() {
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved) {
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
transform.Translate(touchDeltaPosition.x * speed , touchDeltaPosition.y * speed, 0);
}
}
}
Alternate touch control (2):
public class FlipHead : $$anonymous$$onoBehaviour {
public float speed = 1f;
Vector2 pos;
void Start ()
{
pos = transform.position;
}
void Update ()
{
pos = transform.position;
if (Input.touchCount > 0) {
Vector2 touchPos = Input.GetTouch (0).position;
transform.position = Vector2.$$anonymous$$oveTowards (pos, Camera.main.ScreenToWorldPoint (touchPos), speed * Time.deltaTime);
Vector2 dir = touchPos - transform.position; //this line is ambiguous according to the compiler stopping the program from a successful build
float angle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
}
Your answer
Follow this Question
Related Questions
Using Touch to Move Pong Paddles 2 Answers
What Can Input.simulateMouseWithTouches Do? 0 Answers
Working with screen's touch limit 0 Answers
When applying a force to an object on Touch, how do I add a *cooldown* to it? 0 Answers
define touch area 2 Answers