Question by
exoneos · Apr 09, 2016 at 01:55 PM ·
instantiateprefabcursorfollow
Problem with Prefab - Following cursor
Hi all , i want to my cube prefab will instantiate on my cursor position when i make a first left click and after that i want to the cube follow my cursor , after , on a second left click i want my cube get a gravity scale equal to 5 but it didn't and i don't know why ^^ ... thx for your help ! :-)
here is my 2 scripts :
public class DropObject : MonoBehaviour {
public Vector3 screenPoint;
public Vector3 offset;
Vector3 pos;
public GameObject Cursor;
public GameObject PrefabCaisse;
public int IsDrag;
private float fireSpellStart = 0f;
private float fireSpellCooldown = 2f;
void Start () {
IsDrag = 0;
}
void Update () {
Vector3 curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0);
Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint) + offset;
Cursor.transform.position = (Vector2)curPosition;
if (Input.GetButtonDown ("Fire1") && Time.time > fireSpellStart + fireSpellCooldown && IsDrag == 0) {
Instantiate (PrefabCaisse, Cursor.transform.position, Quaternion.identity);
fireSpellStart = Time.time;
IsDrag = 1;
} else if (Input.GetButtonDown ("Fire1") && IsDrag == 1) {
IsDrag = 2;
}
}
public class DragAndDropMover : MonoBehaviour {
public Vector3 screenPoint;
public Vector3 offset;
Vector3 pos;
bool Ignore;
// bool IsOver;
public int IsDrags;
public GameObject PrefabCaisse;
void Start ()
{
IsDrags = 1;
}
// Update is called once per frame
void Update () {
GameObject DropObj = GameObject.Find ("GameManager");
DropObject DropObjScript = DropObj.GetComponent<DropObject> ();
IsDrags = DropObjScript.IsDrag;
Debug.Log (IsDrags);
if(IsDrags == 1) {
Dragged();
}
if (IsDrags == 2) {
PrefabCaisse.GetComponent<Rigidbody2D> ().gravityScale = 5;
}
void Dragged()
{
Vector3 curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y,0);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
PrefabCaisse.gameObject.transform.position = (Vector3) curPosition;
GameObject player = GameObject.Find ("Player");
Player PlayerScript = player.GetComponent<Player> ();
Physics2D.IgnoreCollision (PrefabCaisse.GetComponent<Collider2D> (),player.GetComponent<PolygonCollider2D>(),Ignore = true);
Physics2D.IgnoreCollision (PrefabCaisse.GetComponent<Collider2D> (),player.GetComponent<BoxCollider2D>(),Ignore = true);
}
}
Comment