Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Feb 17 at 01:26 PM by Barere for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Barere · Feb 15 at 11:30 PM · raycastmeshverticesdeformation

How to find smallest length between hit point and vertices

I wrote code for deforming object by raycast, but something I don't know so my code doesn't work. I wanted to find closest point to hit.point by finding smallest length between hit.point and each vertices but then at least inside radius.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LaserGunRayCast : MonoBehaviour
 {
     //raycast
     public float _LaserLenght = 100;
     public float _MaxDistanceCollision = 1000;
     RaycastHit hit;
     float _duration = 10.0f;
     //
     new Vector3 _LaserStart;
     new Vector3 _LaserEnd;
     Color _LaserColor = Color.red;
     public float duration = 5f;
     //
     public float force = 0.1f;
     //public float forceOffset = 0.1f;
     Vector3 _Lenght; 
     //
     void Start()
     { 
     }
 
     void Update()
     {
         Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
         Debug.DrawRay(ray.origin , ray.direction * _LaserLenght,Color.blue);
         if (Physics.Raycast(ray.origin, ray.direction * _LaserLenght, out hit, _MaxDistanceCollision))
         {
             if(Input.GetButtonDown("Fire1"))
             {
             Debug.DrawRay(ray.origin , ray.direction * _LaserLenght,Color.red);
             //ShowLaser(_LaserStart,_LaserEnd,_LaserColor,duration);
             Deformation(hit);
             }
         }
         
     }
     void ShowLaser(Vector3 start, Vector3 end, Color color, float duration)
     {
         GameObject _Laser_GameObject = new GameObject();
 
         _Laser_GameObject.AddComponent<LineRenderer>();
 
         LineRenderer _Laser = _Laser_GameObject.GetComponent<LineRenderer>();
 
         _Laser.SetColors(color, color);
 
         _Laser.SetWidth(0.5f, 0.5f);
 
         _Laser.material = new Material(Shader.Find("Unlit/NewUnlitShader"));
         Debug.Log(_Laser.material);
 
         Vector3 NewTranformPos = new Vector3(transform.position.x + 0.2f,transform.position.y - 0.2f,transform.position.z);
         _Laser.SetPosition(0,NewTranformPos);
         _Laser.SetPosition(1,transform.TransformDirection(Vector3.forward) * 10);
 
         GameObject.Destroy(_Laser_GameObject,duration);
     }
     void Deformation(RaycastHit hit)
     {
         Mesh _deformingMesh = hit.collider.gameObject.GetComponent<MeshFilter>().mesh;
 
         GameObject _GameObjectDeformingMesh = hit.collider.gameObject;
 
         Rigidbody _DeformRigidbody = hit.collider.gameObject.GetComponent<Rigidbody>();
 
         //_DeformRigidbody.AddForce(transform.up * 2f);
         
 
         Vector3[] _MeshVertices = _deformingMesh.vertices; 
         //
         Vector3[] _MeshNormals = _deformingMesh.normals;
 
         int[] _MeshTriangles = _deformingMesh.triangles;
 
     //problems begins right here
         int i;
         decimal _LenghtclosestVertice = 914337593543950333;
         int _IndexClosestVertice = 0;
         int _radius = 10;
         
         for (i = 0; i < _MeshVertices.Length; i++)
         {
        
             if(_radius > (decimal)Vector3.Distance(hit.point,_MeshVertices[i]))
             {
             _MeshVertices[i] -= hit.normal.normalized * force; 
             }
             Debug.DrawRay(hit.point,hit.normal,Color.green,duration);
         }

         _deformingMesh.vertices = _MeshVertices;
         _deformingMesh.RecalculateNormals();
         _deformingMesh.RecalculateBounds();
         
         Destroy(_GameObjectDeformingMesh.GetComponent<MeshCollider>());
         _GameObjectDeformingMesh.AddComponent<MeshCollider>().convex = true;
     }
 }
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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Barere · Feb 17 at 01:25 PM

Well, I found solution. I didn't change hit.point into local coordinates where locate mesh vertices, if I understood it right.

 Vector3 _HitPointLocalCoor = _GameObjectDeformingMesh.transform.InverseTransformPoint(hit.point);
 
         for (int i = 0; i < _MeshVertices.Length; i++)
         {
             float _Distance = (float)Vector3.Distance(_HitPointLocalCoor ,_MeshVertices[i]);
             if(_radius > _Distance)
             {
             _MeshVertices[i] -= hit.normal.normalized * _force;
             }
             Debug.DrawRay(hit.point,hit.normal,Color.green,duration);
         }
Comment
Add comment · Share
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

Follow this Question

Answers Answers and Comments

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

Vertex Mesh Optimization Question 1 Answer

Why is my mesh peeling off when I try to deform it during runtime? 1 Answer

Set Y points of vertices on ground. 1 Answer

How to find connected mesh triangles? 2 Answers

Mash Manipulation like in real world? 0 Answers


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