Quantcast
Channel: Answers by "shriya"
Viewing all articles
Browse latest Browse all 98

Answer by shriya

$
0
0
Here you go . Its using swiping with mouse and sweep test & also iTween. iTween script is attached. Place it in your Standard Assets and you are good to go.[link text][1] using UnityEngine; using UnityEngine.UI; using System.Collections; public class Grid : MonoBehaviour { Vector2 firstPressPos; Vector2 secondPressPos; Vector2 currentSwipe; GameObject selected_Object; Vector3 pos1; Vector3 pos2; GameObject item1; GameObject item2; GameObject yup; GameObject ydown; GameObject xleft; GameObject xright; private bool touchdetection ; void Update() { Swipe (); } void SwapTwoItems(GameObject a, GameObject b) { if(a!=null && b!= null ) { touchdetection = true; iTween.Defaults.easeType = iTween.EaseType.easeOutBack; iTween.MoveTo(a, pos2, 0.2f); iTween.MoveTo(b, pos1, 0.2f); } else { xleft = null; yup = null; ydown = null; xright = null; item1 = null; item2 = null; pos1 = Vector3.zero; pos2 = Vector3.zero; selected_Object = null; touchdetection = false; skip_Swiping = false; } } public void Swipe() { if (Input.GetMouseButtonDown (0) && !touchdetection ) { RaycastHit hitInfo = new RaycastHit(); bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo); if (hit) { print("selected object"+hitInfo.transform.gameObject); selected_Object = hitInfo.transform.gameObject; item1 = selected_Object; } else { print ("No selected object"); } firstPressPos = new Vector2(Input.mousePosition.x,Input.mousePosition.y); } if(Input.GetMouseButtonUp(0)&& !touchdetection) { //save ended touch 2d point if(selected_Object!= null) { secondPressPos = new Vector2(Input.mousePosition.x,Input.mousePosition.y); //create vector from the two points currentSwipe = new Vector2(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y); //normalize the 2d vector currentSwipe.Normalize(); //swipe upwards if(currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f) { Debug.Log("up swipe"); RaycastHit hit1; if (selected_Object.GetComponent().SweepTest(transform.up, out hit1, 2)) { yup = hit1.collider.gameObject; } if(yup!= null) { item2 =yup; pos1 = selected_Object.transform.position; pos2 = item2.transform.position; SwapTwoItems( item1, item2); } } //swipe down if(currentSwipe.y < 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f) { Debug.Log("down swipe"); RaycastHit hit2; if (selected_Object.GetComponent().SweepTest(-transform.up, out hit2, 2)) { ydown = hit2.collider.gameObject; } if(ydown!= null) { item2 = ydown; pos1 = selected_Object.transform.position; pos2 = item2.transform.position; SwapTwoItems( item1, item2); } } //swipe left else if(currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f) { Debug.Log("left swipe"); RaycastHit hit3; if (selected_Object.GetComponent().SweepTest(-transform.right, out hit3, 2)) { xleft = hit3.collider.gameObject; } if(xleft!= null) { item2 = xleft; pos1 = selected_Object.transform.position; pos2 = item2.transform.position; SwapTwoItems( item1, item2); } } //swipe right else if(currentSwipe.x > 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f) { RaycastHit hit4; if (selected_Object.GetComponent().SweepTest(transform.right, out hit4, 2)) { xright = hit4.collider.gameObject; } if(xright!= null) { Debug.Log("right swipe"); item2 = xright; pos1 = selected_Object.transform.position; pos2 = item2.transform.position; SwapTwoItems( item1, item2); } } } } } Its a very simple code for swiping with mouse . If any problem do ask me :) [1]: /storage/temp/57178-itweencs.zip

Viewing all articles
Browse latest Browse all 98

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>