Trouble Creating a Procedural Mesh on a Rotated Game Object

Hello everyone, I’m having trouble creating a procedural mesh on any Game Object that has a rotation other than global (0,0,0).

What is Supposed to Happen:

The script is essentially building a FOV/AOI mesh for detecting objects within that zone. It takes 4 Vector3 points (which essentially becomes a near clip plane). From there, the script makes a raycast from that point (with additional parameters), then gets a point along the raycast and then that data is turned into vertices for the mesh (the Far Clip Plane).

The Problem:

The script seems to work fine when the GO rotation is (0,0,0), but as soon as as the object is rotated (which would happen before the script is run), the mesh begins to skew in the direction of the rotation (ex. rotating the GO to the right (+Y) causes the mesh to skew in that direction). Eventually at 180 degrees, the mesh skews so much it points in the opposite direction of the GO.

Ideas on what is going wrong:

  1. Maybe (probably) incorrectly using AngleAxis in a way that manifests itself when a global rotation is not (0,0,0).

  2. Since I’m just feeding in Vector3 points, I’m actually not sure how the base points end up in the correct place regardless of GO position or rotation. Perhaps I need to make them local?

Any ideas are greatly appreciated.

*UPDATE:

I think I’ve got it working by changing the following 2 lines from transform.forward to Vector3.forward: (everything seems to work now)

		tempRay.direction = Quaternion.AngleAxis(desiredLeftRightAngle, Vector3.up) * (Vector3.forward);
		tempRay.direction += Quaternion.AngleAxis(desiredUpDownAngle, Vector3.right) * (Vector3.forward);

using UnityEngine;
using System.Collections;
using System.Linq;

public class Ship_GunFOVBuilder: MonoBehaviour {
	//responisble for creating the mesh the gun will use to determine what targets it cares about
	//placed on some EMPTY??? base hierarchy object

	//10 16 2014, this script has been combined with another scipt found on Unity Wiki for making procedural cubes...

	public Material mat1; //debugging, used to render object.

	private Ship_GunGroup gunGroup; //need to take some values for each gun from the gun group
	private GameObject gunFOVGO; //child GO on which the components are added to.


	void Start()
	{
		gunGroup = gameObject.GetComponentInParent<Ship_GunGroup>();
		gunFOVGO = transform.GetChild(0).gameObject;
		gunFOVGO.layer = 2; //ignore raycast layer.
		Debug.Log("gunFOVGO.name is: " + gunFOVGO.name);
		
		MeshFilter meshFilter = gunFOVGO.AddComponent<MeshFilter>();
		meshFilter.mesh = MeshBuilder();
		
		MeshRenderer meshRenderer = gunFOVGO.AddComponent<MeshRenderer>();
		meshRenderer.material = mat1;
		meshRenderer.enabled = true; //as long as it works, it does not need to be drawn (collider box will show its position).
	
		//for actual deployment:
		meshRenderer.enabled = false; //as long as it works, it does not need to be drawn (collider box will show its position).

		MeshCollider meshCollider = gunFOVGO.AddComponent<MeshCollider>();
		meshCollider.isTrigger = true;

		Rigidbody rigidbody = gunFOVGO.AddComponent<Rigidbody>();
		rigidbody.isKinematic = true;
		rigidbody.useGravity = false;

		//gunFOVGO.AddComponent<Ship_DeriveAIGunTarget>(); //don't need a reference to it

		//moving the created GO and mesh to a parent GO (in this case the GO that created the new GO), then making the local pos and rot (0,0,0).

		//11 6 2014 no longer needed because the child GO already has these settings by default///
		//gunFOVGO.transform.parent = gameObject.transform;
		//gunFOVGO.transform.localPosition = Vector3.zero;
		//gunFOVGO.transform.localRotation = Quaternion.identity;
		//
	}
	
	
	private Mesh MeshBuilder()
	{
		Mesh newMesh = new Mesh();
		newMesh.name = "FOV mesh";
		newMesh.Clear();
		
		//float length = 1f;
		//float width = 1f;
		//float height = 1f;
		
		#region Vertices
		//FRONT ->  input from gunGroup
		//top left
		//Vector3 p0 = new Vector3( -length * .5f,	-width * .5f, height * .5f );
		Vector3 p0 = gunGroup.fovTopLeftBase;
		
		//top right
		//Vector3 p1 = new Vector3( length * .5f, 	-width * .5f, height * .5f );
		Vector3 p1 = gunGroup.fovTopRightBase;
		
		//bottom right
		//Vector3 p2 = new Vector3( length * .5f, 	-width * .5f, -height * .5f );
		Vector3 p2 = gunGroup.fovBotRightBase;
		
		//bottom left
		//Vector3 p3 = new Vector3( -length * .5f,	-width * .5f, -height * .5f );
		Vector3 p3 = gunGroup.fovBotLeftBase;
		
		//BACK -> calculated(not sure if should look at back from through front, or only back...So I'm not sure how to name these points...
		//top left
		//Vector3 p4 = new Vector3( -length * .5f,	width * .5f,  height * .5f );
		Vector3 p4 = CalculateNextVertice(p0, gunGroup.fovUpAngle, gunGroup.fovLeftAngle, gunGroup.fovDistance);
		
		//top right
		//Vector3 p5 = new Vector3( length * .5f, 	width * .5f,  height * .5f );
		Vector3 p5 = CalculateNextVertice(p1, gunGroup.fovUpAngle, gunGroup.fovRightAngle, gunGroup.fovDistance);
		
		//bottom right
		//Vector3 p6 = new Vector3( length * .5f, 	width * .5f,  -height * .5f );
		Vector3 p6 = CalculateNextVertice(p2, gunGroup.fovDownAngle, gunGroup.fovRightAngle, gunGroup.fovDistance);
		
		//bottom left
		//Vector3 p7 = new Vector3( -length * .5f,	width * .5f,  -height * .5f );
		Vector3 p7 = CalculateNextVertice(p3, gunGroup.fovDownAngle, gunGroup.fovLeftAngle, gunGroup.fovDistance);
		
		
		Vector3[] vertices = new Vector3[]
		{
			// Bottom
			p0, p1, p2, p3,
			
			// Left
			p7, p4, p0, p3,
			
			// Front
			p4, p5, p1, p0,
			
			// Back
			p6, p7, p3, p2,
			
			// Right
			p5, p6, p2, p1,
			
			// Top
			p7, p6, p5, p4
		};
		#endregion
		
		#region Normales
		Vector3 up 	= Vector3.up;
		Vector3 down 	= Vector3.down;
		Vector3 front 	= Vector3.forward;
		Vector3 back 	= Vector3.back;
		Vector3 left 	= Vector3.left;
		Vector3 right 	= Vector3.right;
		
		Vector3[] normales = new Vector3[]
		{
			// Bottom
			down, down, down, down,
			
			// Left
			left, left, left, left,
			
			// Front
			front, front, front, front,
			
			// Back
			back, back, back, back,
			
			// Right
			right, right, right, right,
			
			// Top
			up, up, up, up
		};
		#endregion	
		
		#region UVs
		Vector2 _00 = new Vector2( 0f, 0f );
		Vector2 _10 = new Vector2( 1f, 0f );
		Vector2 _01 = new Vector2( 0f, 1f );
		Vector2 _11 = new Vector2( 1f, 1f );
		
		Vector2[] uvs = new Vector2[]
		{
			// Bottom
			_11, _01, _00, _10,
			
			// Left
			_11, _01, _00, _10,
			
			// Front
			_11, _01, _00, _10,
			
			// Back
			_11, _01, _00, _10,
			
			// Right
			_11, _01, _00, _10,
			
			// Top
			_11, _01, _00, _10,
		};
		#endregion
		
		#region Triangles
		int[] triangles = new int[]
		{
			// Bottom
			3, 1, 0,
			3, 2, 1,			
			
			// Left
			3 + 4 * 1, 1 + 4 * 1, 0 + 4 * 1,
			3 + 4 * 1, 2 + 4 * 1, 1 + 4 * 1,
			
			// Front
			3 + 4 * 2, 1 + 4 * 2, 0 + 4 * 2,
			3 + 4 * 2, 2 + 4 * 2, 1 + 4 * 2,
			
			// Back
			3 + 4 * 3, 1 + 4 * 3, 0 + 4 * 3,
			3 + 4 * 3, 2 + 4 * 3, 1 + 4 * 3,
			
			// Right
			3 + 4 * 4, 1 + 4 * 4, 0 + 4 * 4,
			3 + 4 * 4, 2 + 4 * 4, 1 + 4 * 4,
			
			// Top
			3 + 4 * 5, 1 + 4 * 5, 0 + 4 * 5,
			3 + 4 * 5, 2 + 4 * 5, 1 + 4 * 5,
			
		};
		#endregion
		
		newMesh.vertices = vertices;
		
		newMesh.normals = normales;
		
		newMesh.uv = uvs;
		
		newMesh.triangles = triangles;
		//reverse them to invert normals (which fixes an issue where all normals are inverted for an unkown reason)...
		newMesh.triangles = newMesh.triangles.Reverse().ToArray();
		
		newMesh.RecalculateBounds();
		
		newMesh.Optimize();
		
		return newMesh;
	}
	
	//using parameters, calculate where the desired point will be:
	private Vector3 CalculateNextVertice(Vector3 startingPoint, float desiredUpDownAngle,  float desiredLeftRightAngle, float desiredDistance)
	{
		Vector3 createdVec3 = new Vector3();
		
		Ray tempRay = new Ray();
		tempRay.origin = startingPoint;
		tempRay.direction = Quaternion.AngleAxis(desiredLeftRightAngle, gunFOVGO.transform.up) * (gunFOVGO.transform.forward);
		tempRay.direction += Quaternion.AngleAxis(desiredUpDownAngle, gunFOVGO.transform.right) * (gunFOVGO.transform.forward);
		
		Physics.Raycast (tempRay.origin, tempRay.direction, desiredDistance);
		Debug.DrawRay (tempRay.origin, tempRay.direction * desiredDistance, Color.red, 5f);
		
		//get the point at the end of the ray:
		createdVec3 = tempRay.GetPoint (desiredDistance);
		
		Debug.Log ("createdVec3 is: " + createdVec3);
		return createdVec3;
	}
}

Couple of ideas:

  1. I can’t see where you are converting the vertices into local space (i.e. InverseTransformPoint). Are you doing this?

  2. If you are doing a conversion, then have you tried disabling the mesh render and drawn debug lines point to point from/to the bounding vertices as a sanity check?