How to stop vertex from moving after raycast hit
HI I have a script that basically create a circle, add mesh and polygon collider. then move the vertices outward (updating the collider and mesh). I then put a circle collider on each vertex so that when (each vertex) hit something it will not move any more. But for some reason this is not working please help.(The vertex is not completely stopping)
have a scene with 3 empty gameobject and this script attached to each*
using UnityEngine; using System.Collections; using System.Collections.Generic;
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))] public class MyGrid : MonoBehaviour {
public float pause; public float Rad; public int MySize; private Vector3[] vertices; private Vector2[] vert2D; private Vector2 pos; private float Ang ; private Mesh mesh; private void Awake () { StartCoroutine(Generate()); } private IEnumerator Generate () { WaitForSeconds wait = new WaitForSeconds(pause); //gets the mesh filter and rename it GetComponent<MeshFilter>().mesh = mesh = new Mesh(); mesh.name = "Procedural Grid"; //assign the Center variable and its position at object center Vector2 Center = transform.position; //creates vertices list vertices = new Vector3[MySize+1]; yield return wait; //put the last vertice in the center (local space !!) vertices [MySize] = transform.InverseTransformPoint(Center); yield return wait; //assighn length to verts2D // vert2D = new Vector2[vertices.Length]; for (int i = 0; i < MySize; i++) { //calculate where the vertices will be (At RandomCircle) Vector2 pos = RandomCircle (Center,Rad); //Assign vertices to position on circle (local space !!) vertices [i] = transform.InverseTransformPoint( pos); yield return wait; } //assign to mesh filter mesh.vertices = vertices; //create triangleslist and fill it int[] triangles = new int[(MySize)*3]; for (int i = 0 , v = 0; i < ((MySize)*3); i+=3 , v++) { triangles [i] = v; if (i == ((MySize * 3) - 3)) { triangles [i + 1] = 0; } else { triangles [i + 1] = v + 1; } triangles [i + 2] = MySize; mesh.triangles = triangles; yield return wait; } } //Calculate the circle points Vector2 RandomCircle (Vector2 Center , float Radius){ //if it is not the first point take the first point + angle else just take the angle if(Ang >= 0.1f){Ang = Ang+(360/((float)MySize));}else {Ang = (360/((float)MySize));} pos.x = Center.x + Radius * Mathf.Sin (Ang * Mathf.Deg2Rad); pos.y = Center.y + Radius * Mathf.Cos (Ang * Mathf.Deg2Rad); return pos; } void Update(){ vert2D = new Vector2[vertices.Length]; //fill vert2 with same as vetices exept for last make same as first for (int a = 0; a < ((vertices.Length) - 1); a++) { vert2D[a] = (vertices [a]); } vert2D[vertices.Length - 1] = ( vertices [0]); for(int i = 0; i < vert2D.Length ; i++){ //make a circular ray around each vertex RaycastHit2D hit = Physics2D.CircleCast (transform.TransformPoint (vert2D [i]), 1, Vector2.zero); //when it collides and it is not itself if (hit.collider != null && hit.collider.name != transform.gameObject.name) { } else { //move all vertices outward vertices [i] += vertices [i] * Time.deltaTime / 10; } //if you dont have a PolygonCollider2D make one if(GetComponent<PolygonCollider2D>() == null){ transform.gameObject.AddComponent<PolygonCollider2D> (); } // set poligon collider points to the verteces PolygonCollider2D testpoint = GetComponent<PolygonCollider2D> (); //recelculate points(colider) vertices(mesh) testpoint.points = vert2D; mesh.vertices = vertices; mesh.RecalculateBounds (); } } //draw a gismo at vertices and gives them colour private void OnDrawGizmos () { if (vertices == null) { return; } Gizmos.color = Color.red; for (int i = 0; i < vertices.Length; i++) { Gizmos.DrawSphere(transform.TransformPoint( vertices[i]), 0.1f); } } }
Your answer
![](https://koobas.hobune.stream/wayback/20220612123657im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Shader for alpha fading 0 Answers
Issue: My Marching Cubes algorithm is adding texture inside cubes. 0 Answers
Cannot edit polygon collider when zooming in 3 Answers
RaycastHit2D ground detection 1 Answer