- Home /
Question by
Subtain · Oct 20, 2017 at 05:26 AM ·
swipedraggingspringjoint
I want to Swipe anywhere on the screen ,that causes my player to move down and then as spring movement moves towards enemy as shown in fig...... i have tried my best butt all to no avail
here's my script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI;
public class Ball : MonoBehaviour {
public Rigidbody2D rb;
Rigidbody2D hook;
public float releaseTime = .15f;
public float maxDragDistance = 2f;
GameObject nextBall,text,Text2;
private bool isPressed = false;
// public static int rem = 6; public static int i = 1; public static int stones = 10,Stars = 0;
float yMin = -1;
float yMax = 1 ;
float xMin = -1;
float xMax = -1;
void Start()
{
text = GameObject.Find ("MainCanvas").transform.GetChild (0).GetChild (1).transform.gameObject;
Text2 = GameObject.Find ("MainCanvas").transform.GetChild (0).GetChild (2).transform.gameObject;
text.GetComponent<Text> ().text = stones.ToString ();
Text2.GetComponent<Text> ().text = Stars.ToString ();
nextBall = transform.parent.transform.GetChild (i).gameObject;
i++;
hook = GameObject.Find ("Hook").transform.GetComponent<Rigidbody2D>();
}
void Update ()
{
// mousePos.x = Mathf.Clamp(mousePos.x, -6.2f,- 6f); // mousePos.y = Mathf.Clamp(mousePos.y, -0.8f, 3.5f);
if (isPressed)
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.x = Mathf.Clamp(mousePos.x, -6.2f,- 6f);
mousePos.y = Mathf.Clamp(mousePos.y, -0.2f,0.8f);
// Debug.Log (mousePos.y); if (Vector3.Distance(mousePos, hook.position) > maxDragDistance) rb.position = hook.position + (mousePos - hook.position).normalized * maxDragDistance; else rb.position = mousePos; } text.GetComponent ().text = stones.ToString (); // PlayerPrefs.SetInt ("Key", Stars);
Stars = PlayerPrefs.GetInt ("Key", Stars);
Text2.GetComponent<Text> ().text = Stars.ToString ();
if (stones == 0) {
GameObject.Find ("MainCanvas").transform.GetChild (2).gameObject.SetActive (true);
}
}
public void OnMouseDown ()
{
isPressed = true;
rb.isKinematic = true;
}
public void OnMouseUp ()
{
isPressed = false;
rb.isKinematic = false;
StartCoroutine(Release());
}
IEnumerator Release ()
{
yield return new WaitForSeconds(releaseTime);
stones--;
GetComponent<SpringJoint2D>().enabled = false;
this.enabled = false;
yield return new WaitForSeconds(2f);
if (nextBall != null)
{
nextBall.SetActive(true);
} else
{
Enemy.EnemiesAlive = 0;
}
}
1.png
(80.2 kB)
Comment