Skip to content

Instantly share code, notes, and snippets.

@Hemanth759
Created June 20, 2020 06:53
Show Gist options
  • Select an option

  • Save Hemanth759/6b63260c9aa6f2a93b8351da9328b3d5 to your computer and use it in GitHub Desktop.

Select an option

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
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