我在过去的几天里一直在努力制作一个可以弯曲的程序生成的网格,也就是一条道路。道路脚本具有使其以给定角度转动的功能,以及使其以给定角度前进的功能。在向前移动后尝试向右转弯时出现问题(如下图所示)。
我不能为我的生活找出我的代码中的问题,但我假设我在某处的x和y坐标上犯了一个会计错误。
using UnityEngine;
using System.Collections;
public class procPlane : MonoBehaviour {
public Mesh mesh;
public Vector3[] vertices;
public int currentVertice;
public Vector3[] normales;
public Vector2[] uvs;
public int[] triangles;
public int currentTriangle;
//current vertice number
public int verticeNumber = 4;
//current x and z outer values.
public float x_out = -0.5f;
public float z_out = 0.1f;
public float angle = 0.0f;
private float x_in = 0.5f;
public float z_in = 0.1f;
public float xi=0.5f, xo=-0.1f, zi=0.1f, zo=0.1f;
// Use this for initialization
void Start () {
// You can change that line to provide another MeshFilter
MeshFilter filter = gameObject.AddComponent< MeshFilter >();
mesh = filter.mesh;
mesh.Clear();
#region Vertices
vertices = new Vector3[1000];
currentVertice = 0;
vertices[currentVertice++] = new Vector3(-0.5f, 0.0f, -0.1f);
vertices[currentVertice++] = new Vector3(0.5f, 0.0f, -0.1f);
vertices[currentVertice++] = new Vector3(-0.5f, 0.0f, 0.0f);
vertices[currentVertice++] = new Vector3(0.5f, 0.0f, 0.0f);
vertices[currentVertice++] = new Vector3(-0.5f, 0.0f, 0.1f);
vertices[currentVertice++] = new Vector3(0.5f, 0.0f, 0.1f);
#endregion
#region Normales
normales = new Vector3[ vertices.Length ];
for( int n = 0; n < normales.Length; n++ )
normales[n] = Vector3.up;
#endregion
#region Triangles
triangles = new int[2*6*100];
currentTriangle = 0;
triangles[currentTriangle++] = 2;
triangles[currentTriangle++] = 1;
triangles[currentTriangle++] = 0;
triangles[currentTriangle++] = 2;
triangles[currentTriangle++] = 3;
triangles[currentTriangle++] = 1;
triangles[currentTriangle++] = 4;
triangles[currentTriangle++] = 3;
triangles[currentTriangle++] = 2;
triangles[currentTriangle++] = 4;
triangles[currentTriangle++] = 5;
triangles[currentTriangle++] = 3;
#endregion
mesh.vertices = vertices;
mesh.normals = normales;
mesh.uv = uvs;
mesh.triangles = triangles;
mesh.RecalculateBounds();
mesh.Optimize();
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.UpArrow))
{
Vector3 A = Quaternion.Euler(0,angle,0)*Vector3.forward*0.1f;
x_out+=A.x;
x_in+=A.x;
z_in += A.z;
z_out += A.z;
Debug.LogWarning(z_in);
xo += A.x;
xi += A.x;
zo += A.z;
zi += A.z;
vertices[currentVertice++] = new Vector3(x_out, 0.0f, z_out);
vertices[currentVertice++] = new Vector3(x_in, 0.0f, z_in);
//normals
normales[currentVertice] = Vector3.up;
//triangles
verticeNumber += 2;
triangles[currentTriangle++] = verticeNumber;
triangles[currentTriangle++] = verticeNumber-1;
triangles[currentTriangle++] = verticeNumber-2;
triangles[currentTriangle++] = verticeNumber;
triangles[currentTriangle++] = verticeNumber+1;
triangles[currentTriangle++] = verticeNumber-1;
mesh.vertices = vertices;
mesh.normals = normales;
mesh.uv = uvs;
mesh.triangles = triangles;
mesh.RecalculateBounds();
mesh.Optimize();
}
if(Input.GetKeyDown(KeyCode.RightArrow))
{
//z_out += z_out;
//z_in += z_in;
angle += 9.0f;
float x_outer = (float)-(2.0f * Mathf.Cos(angle*Mathf.PI/180.0f))+1.5f;
float z_outer = (float)(2.0f * Mathf.Sin(angle*Mathf.PI/180.0f));
x_out = x_outer;
//Debug.LogWarning(z_out);
z_out = zo + z_outer;
float x_inner = (float)-(1.0f * Mathf.Cos(angle*Mathf.PI/180.0f))+1.5f;
float z_inner = (float)(1.0f * Mathf.Sin(angle*Mathf.PI/180.0f))-0.1f;
//x_in = x_in - (x_inner+(x_out));
x_in = x_inner;
z_in = zi + z_inner;
Debug.LogWarning(zi);
Debug.LogWarning(z_inner);
//outer vertice
vertices[currentVertice++] = new Vector3(x_out, 0.0f, z_out);
//inner vertice
vertices[currentVertice++] = new Vector3(x_in, 0.0f, z_in);
//normals
normales[currentVertice] = Vector3.up;
//triangles
verticeNumber += 2;
triangles[currentTriangle++] = verticeNumber;
triangles[currentTriangle++] = verticeNumber-1;
triangles[currentTriangle++] = verticeNumber-2;
triangles[currentTriangle++] = verticeNumber;
triangles[currentTriangle++] = verticeNumber+1;
triangles[currentTriangle++] = verticeNumber-1;
mesh.vertices = vertices;
mesh.normals = normales;
mesh.uv = uvs;
mesh.triangles = triangles;
mesh.RecalculateBounds();
mesh.Optimize();
}
}
}
答案 0 :(得分:0)
不确定但您可能遇到使用Quaternion.Euler的Gimbal Lock问题。当数学变得混乱时,检查你的角度是什么。
检查一下......
http://answers.unity3d.com/questions/573035/avoiding-gimbal-lock.html
赖安