Created
June 20, 2020 06:53
-
-
Save Hemanth759/6b63260c9aa6f2a93b8351da9328b3d5 to your computer and use it in GitHub Desktop.
The Unity C# script to deform the game object to implement the physics of sand balls game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| public class PlaneDeformer : MonoBehaviour | |
| { | |
| //public references | |
| public float radiusOfDeformation; | |
| public float powerOfDeformation; | |
| public GameObject cylinderPrefab; | |
| // private references | |
| MeshFilter meshFilter; | |
| Mesh mesh; | |
| MeshCollider col; | |
| Vector3[] verts; | |
| private void Awake() | |
| { | |
| meshFilter = this.GetComponent<MeshFilter>(); | |
| col = this.GetComponent<MeshCollider>(); | |
| mesh = meshFilter.mesh; | |
| verts = mesh.vertices; | |
| } | |
| public void deformThePlane(Vector3 positionToDeform) | |
| { | |
| Vector3 hitpoint = positionToDeform; | |
| positionToDeform = this.transform.InverseTransformPoint(positionToDeform); | |
| bool somethingDeformed = false; | |
| for (int i = 0; i < verts.Length; i++) | |
| { | |
| float dist = (verts[i] - positionToDeform).sqrMagnitude; | |
| if (dist < radiusOfDeformation) | |
| { | |
| verts[i] -= Vector3.up * powerOfDeformation; | |
| somethingDeformed = true; | |
| } | |
| } | |
| if (somethingDeformed) | |
| { | |
| mesh.vertices = verts; | |
| col.sharedMesh = mesh; | |
| // Instantiate(cylinderPrefab, new Vector3(hitpoint.x, hitpoint.y, hitpoint.z + 0.11f), Quaternion.Euler(-90f, 0f, 0f)); | |
| } | |
| } | |
| public void puthole(Vector3 positionToDeform, float radius) | |
| { | |
| Vector3 hitpoint = positionToDeform; | |
| positionToDeform = this.transform.InverseTransformPoint(positionToDeform); | |
| bool somethingDeformed = false; | |
| for (int i = 0; i < verts.Length; i++) | |
| { | |
| float dist = (verts[i] - positionToDeform).sqrMagnitude; | |
| if (dist < radius) | |
| { | |
| verts[i] -= Vector3.up * powerOfDeformation; | |
| somethingDeformed = true; | |
| } | |
| } | |
| if (somethingDeformed) | |
| { | |
| mesh.vertices = verts; | |
| col.sharedMesh = mesh; | |
| // Instantiate(cylinderPrefab, new Vector3(hitpoint.x, hitpoint.y, hitpoint.z + 0.11f), Quaternion.Euler(-90f, 0f, 0f)); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment