Hello, I am making a VR welding game and want some help generating a bid effect and glue effect in two scenes using mesh generation.
I have created a mesh generation script that looks somewhat like this GIF in a prototype I made:-
I am using this link as a reference to show how It should look:-
Reference Effect
Can someone help in making my generation better (just like the ref link) and provide some reference code or tutorials to make the mesh thicker like a fluid?
Here is the mesh generator script I’m using:-
GlueMeshGenerator.cs (2.3 KB)
using System.Linq;
using UnityEngine;
public class GlueMeshGenerator : MonoBehaviour
{
public Transform glueGunTip;
public float glueWidth = 0.1f;
public float glueHeight = 0.05f;
public Transform glueGun;
private Mesh glueMesh;
private Vector3 lastPosition;
void Start()
{
glueMesh = new Mesh();
GetComponent<MeshFilter>().mesh = glueMesh;
lastPosition = glueGunTip.position;
}
void Update()
{
if (Input.GetKey(KeyCode.W))
{
glueGun.transform.position = new Vector3(glueGun.position.x, glueGun.position.y, glueGun.position.z + 0.05f);
}
if (Input.GetKey(KeyCode.A))
{
glueGun.transform.position = new Vector3(glueGun.position.x - 0.05f, glueGun.position.y, glueGun.position.z);
}
if (Input.GetKey(KeyCode.S))
{
glueGun.transform.position = new Vector3(glueGun.position.x, glueGun.position.y, glueGun.position.z - 0.05f);
}
if (Input.GetKey(KeyCode.D))
{
glueGun.transform.position = new Vector3(glueGun.position.x + 0.05f, glueGun.position.y, glueGun.position.z);
}
if (Vector3.Distance(lastPosition, glueGunTip.position) > 0.1f)
{
AddGlueSegment(lastPosition, glueGunTip.position);
lastPosition = glueGunTip.position;
}
}
void AddGlueSegment(Vector3 start, Vector3 end)
{
Vector3 direction = (end - start).normalized;
Vector3 perpendicular = Vector3.Cross(direction, Vector3.up).normalized;
Vector3[] newVertices = new Vector3[4];
newVertices[0] = start + perpendicular * glueWidth * 0.5f;
newVertices[1] = start - perpendicular * glueWidth * 0.5f;
newVertices[2] = end + perpendicular * glueWidth * 0.5f;
newVertices[3] = end - perpendicular * glueWidth * 0.5f;
int[] newTriangles = new int[] { 0, 2, 1, 2, 3, 1 };
int vertexOffset = glueMesh.vertices.Length;
glueMesh.vertices = glueMesh.vertices.Concat(newVertices).ToArray();
glueMesh.triangles = glueMesh.triangles.Concat(newTriangles.Select(t => t + vertexOffset)).ToArray();
glueMesh.RecalculateNormals();
}
}