//Activates pulling effect of grenade void ActivateGrenade() { colliders = Physics.OverlapSphere(transform.position, radius); foreach (Collider hit in colliders) { //Gets Gameobject of collider and its components GameObject go = hit.transform.gameObject; WallBlockScript cubeScript = go.GetComponent <WallBlockScript> (); //Calculates position of the object that is affected Vector3 posOfHitObj = hit.gameObject.transform.position; //Calculates the direction in which object will be pulled Vector3 directionOfPull = transform.position - posOfHitObj; //Unfreezes the rigibody of the cube + unfreezes it Rigidbody rb = hit.GetComponent <Rigidbody> (); if (rb != null && cubeScript != null) { rb.AddForce(attractionStrenght * directionOfPull); cubeScript.UnFreezeRb(); rb.useGravity = false; } } }
// Use this for initialization void Start() { for (int x = 0; x < wallSizeX; x++) { for (int y = 0; y < wallSizeY; y++) { //Creates the cube and puts in postion + attaches it to the parent object (this)(Wall) GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.parent = transform; cube.transform.position = new Vector3(x, y, 0); //Adds a WallBlockScript and Rigidbody to the cube cube.AddComponent <Rigidbody> (); WallBlockScript cubeScript = cube.AddComponent <WallBlockScript> (); cubeScript.freezeRb(); //Sets the default material to cube cube.GetComponent <Renderer> ().material = materialRef; } } }
void GravityActive() { if (!gotObjects) { GetObjectsInRadius(); gotObjects = true; Debug.Log("Got Objects to pull"); } if (gravPullObjects != null) { foreach (Collider col in gravPullObjects) { GameObject go = col.transform.gameObject; WallBlockScript cubeScript = go.GetComponent <WallBlockScript> (); //Calculates position of the object that is affected Vector3 posOfHitObj = col.gameObject.transform.position; //Calculates the direction in which object will be pulled Vector3 directionOfPull = objectHoverPos - posOfHitObj; //Unfreezes the rigibody of the cube + unfreezes it Rigidbody rb = col.GetComponent <Rigidbody> (); if (rb != null && cubeScript != null && rb.tag != "gravPullArea") { Debug.Log("Pulling Objects"); cubeScript.UnFreezeRb(); rb.AddForce(attractionStrenght * directionOfPull); rb.useGravity = false; } } } }