- 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
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
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);
}