# New to game dev, understanding sine function oscillation

I am very new to game dev and I’m struggling to get my scripts to do what I want. It seems I’m missing a critical piece of information or failing to understand how something works here. I am trying to understand meshes and the direct manipulation of vertices. Right now, I have a cylinder vertically aligned along the y-axis. What I am trying to do is get all of the vertices of one of the cylinder’s circular face and manipulate them so they grow and shrink periodically. Currently, my code looks like this:

``````void Update()
{

Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;

for (var i = 0; i < vertices.Length; i++)
{
var updateThis = (Mathf.Sin(Time.time + 1f));
if (vertices[i][1] > 0)
{
vertices[i] = new Vector3(vertices[i][0] * Mathf.Sin(Time.time + 1f), vertices[i][1], vertices[i][2] * Mathf.Sin(Time.time + 1f));
}
else
{

}
}

mesh.vertices = vertices;
}
``````

However, when I run this, the sine function is exhibiting some strange behavior. The script does get all of the vertices of the “top” face, and shrink them towards the face’s center, but it will not grow them back to their original position. The vertices all seem, for lack of a better term, to get stuck at the center of the face. Why won’t they “grow” back to their original position and how can I fix this?

When you’re doing a relative offset, you need to make sure you’re performing the offset relative to a fixed position. You are modifying the vertices but then throwing away the original positions. Save the original positions and modify the originals by Sin every frame, rather than the transformed vertices.

2 Likes

Thank you so much! This did the trick! I would never have been able to figure that out on my own.