Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Runtime Mesh Deformation NOT Working!

Discussion in 'Editor & General Support' started by funkoff, May 28, 2020.

?

How Do I Make It Work?

  1. There is a Bug in Code

    0 vote(s)
    0.0%
  2. There is a runtime error.

    0 vote(s)
    0.0%
Multiple votes are allowed.
  1. funkoff

    funkoff

    Joined:
    May 28, 2020
    Posts:
    1
    img1.png img2.png
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class cameraHandler : MonoBehaviour
    6. {
    7.     Ray ray;
    8.     RaycastHit hit;
    9.  
    10.     [SerializeField]
    11.     GameObject plane;
    12.  
    13.     Camera cam;
    14.  
    15.     void Start()
    16.     {
    17.         cam = transform.GetComponent<Camera>();
    18.     }
    19.  
    20.     void FixedUpdate()
    21.     {
    22.         if (Input.GetMouseButtonDown(0))
    23.         {
    24.             DeformMesh();
    25.         }
    26.     }
    27.  
    28.     void DeformMesh()
    29.     {
    30.         ray = cam.ScreenPointToRay(Input.mousePosition);
    31.  
    32.         if(Physics.Raycast(ray, out hit))
    33.         {
    34.             DeformPlane deformPlane = plane.transform.GetComponent<DeformPlane>();
    35.             deformPlane.DeformThisPlane(hit.point);
    36.         }
    37.     }
    38. }
    img1.png img2.png
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DeformPlane : MonoBehaviour
    6. {
    7.     MeshFilter meshFilter;
    8.     Mesh PlaneMesh;
    9.     Vector3[] verts;
    10.  
    11.     [SerializeField] float Radius = 4f;
    12.  
    13.     [SerializeField] float power = 20f;
    14.  
    15.     void Start()
    16.     {
    17.         meshFilter = GetComponent<MeshFilter>();
    18.         PlaneMesh = meshFilter.mesh;
    19.         verts = PlaneMesh.vertices;
    20.     }
    21.  
    22.     public void DeformThisPlane(Vector3 positionToDeform)
    23.     {
    24.         positionToDeform = transform.InverseTransformPoint(positionToDeform);
    25.  
    26.         for(int i=0; i < verts.Length; i++)
    27.         {
    28.             float dist = (verts[i] - positionToDeform).sqrMagnitude;
    29.  
    30.             if(dist == Radius)
    31.             {
    32.                 verts[i] -= Vector3.up * power;
    33.             }
    34.         }
    35.         PlaneMesh.vertices = verts;
    36.     }
    37. }
    38.  
     
  2. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    How exactly is it "NOT Working"?

    Console errors? Compile errors? No errors but nothing happens? Give us something to work with here.

    Have you made any attempt to debug it yourself? e.g. added Debug.Log statements to verify that your DeformThisPlane function is being called?