Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by cobra050 · Jun 07, 2017 at 06:45 AM · verticesraycasthit2dpolygon collider 2d

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);
             }
         }
     }
    
    
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

106 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to know (better by GUI) the average nb of Triangles, Average Number of Vertices and Average nb Draw Calls in a game? 0 Answers

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges