public override void _Process(float delta) { if (shouldCalcNew && ((int)walkedDistance < (int)(walkedDistance + delta * MovementSpeed) || path == null)) { shouldCalcNew = false; float gridSize = this.grid3d.GetGridSize(); PointI nextGrid = new PointI((int)(Translation.x / gridSize), (int)(Translation.z / gridSize)); if (pathFinder.FindPath(out points, nextGrid)) { if (points.Length > 1) { path = new SplinePath(points, grid3d.GetGridSize(), InterpolationType.Qubic); } Blocked = false; exclamationInst.Visible = false; } else { Blocked = true; path = null; walkedDistance = 0; exclamationInst.Visible = true; } walkedDistance = 0; } if (path != null) { Vector2 pos = path.GetPoint(walkedDistance); walkedDistance += delta * MovementSpeed; this.Translation = new Vector3(pos.x, 0, pos.y); } }
// Update is called once per frame void Update() { if (IsFollowing) { var frac = (Mathf.Sin(Time.time * 2f) + 1f) / 2f; Frac = frac; transform.position = Path.GetPoint(frac); } }
public void BuildMesh() { if (spline == null) { spline = GetComponent <SplinePath>(); } mf = GetComponent <MeshFilter>(); meshCollider = GetComponent <MeshCollider>(); if (meshCollider == null) { meshCollider = gameObject.AddComponent <MeshCollider>(); } if (mf && spline) { if (mf.sharedMesh != null) { DestroyImmediate(mf.sharedMesh); } if (mesh == null) { mesh = new Mesh(); } verts.Clear(); indices.Clear(); normals.Clear(); Vector3 lastPos = spline.GetPoint(0f); List <float> iters = new List <float>(); iters.Add(0f); float iter = 0f; while (iter < 1f) { float moveDist = spacing; while (moveDist > 0f && iter < 1f) { float prevIter = iter; iter += alphaIter; iter = Mathf.Clamp01(iter); Vector3 pos = spline.GetPoint(iter); Vector3 toPos = pos - lastPos; float toPosDist = toPos.magnitude; if (toPosDist < moveDist) { moveDist -= toPosDist; lastPos = pos; } else { float alpha = toPosDist / moveDist; iter = Mathf.Lerp(prevIter, iter, alpha); lastPos = Vector3.Lerp(lastPos, pos, alpha); iters.Add(iter); moveDist = 0f; } } } iters[iters.Count - 1] = 1f; for (int i = 0; i < iters.Count; i++) { AddSideFaces(iters[i], i); } if (arcAmount < 1f) { for (int i = 0; i < iters.Count; i++) { AddTopFace(iters[i], i); } } AddFrontBackFaces(iters[0], 0); AddFrontBackFaces(iters[iters.Count - 1], iters.Count - 1); mesh.SetVertices(verts); mesh.SetTriangles(indices, 0); mesh.SetNormals(normals); mf.sharedMesh = mesh; meshCollider.sharedMesh = mesh; OnMeshChanged(this); } }