Search Unity

Odd results after creating plane on bezier curve. Then setting new vector3 position of game object.

Discussion in 'Scripting' started by ReggieBeRetro, May 11, 2017.

  1. ReggieBeRetro

    ReggieBeRetro

    Joined:
    Aug 30, 2015
    Posts:
    73
    Hello

    So my problem is after my curve is created I am having issues setting the transform position of the object to place the curve/road in the right location.

    When I set a new vector3 of the transform of the object to 0 or (BentX, BentY, BentZ) when they equal 0 the plane is created where it should. but when i try to replace the vector3 x,y and z to BentSpline.transform.position.x ,y and z the plane ends up way out in left field and off the terrain and not even close the where it should be. It always ends up down and to the left. Its like something is being added on it them mean the x,y and z transform positions.

    If you take a look at the update function you will see a commented line. Mesh and gameObject are the same thing.

    Any help or input is appreciated.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. [ExecuteInEditMode]
    7. public class BendObj : MonoBehaviour
    8. {
    9.    
    10.     public Texture myTexture;
    11.     [SerializeField]
    12.     public GameObject MeshPath;
    13.     public GameObject BentSpline;
    14.     public bool locSet = false;
    15.     public int totalroads = 0;
    16.     public int detailLvl = 60; //the higher the soother
    17.     public int roadWidth;
    18.     public float positive = 0;
    19.     public float negative = 0;
    20.     public float BentX;
    21.     public float BentY;
    22.     public float BentZ;
    23.     public Mesh mesh;
    24.  
    25.     public void Update() {
    26.        
    27.         BentSpline = GameObject.Find("rSpline");
    28.         MeshPath = GameObject.Find("PathPlane");
    29.            
    30.         if (BentSpline != null) {
    31.             if (MeshPath != null) {
    32.                 BentX = 0f;
    33.                 BentY = 0f;
    34.                 BentZ = 0f;
    35.                 gameObject.transform.position = new Vector3 (BentSpline.transform.position.x, BentSpline.transform.position.y, BentSpline.transform.position.z);
    36.  
    37.                 //MeshPath.transform.position = new Vector3 (Math.Abs(BentX), Math.Abs(BentY), -Math.Abs(BentZ));
    38.             }
    39.                     }
    40.  
    41.    
    42.  
    43.         }
    44.     public void Start()    {
    45.         RaycastHit hit = new RaycastHit ();
    46.         BentSpline = GameObject.Find("rSpline");
    47.         MeshPath = GameObject.Find("PathPlane");
    48.  
    49.         locSet = false;
    50.         //Mesh mesh = new Mesh();
    51.         List<Vector3> vertices = new List<Vector3>();
    52.         List<int> triangles = new List<int>();
    53.         List<Vector3> normals = new List<Vector3>();
    54.         Vector3 Start = GetPoint(0f);
    55.    
    56.         Quaternion rotation = GetRotation(0);
    57.         Vector3 left = rotation * Vector3.left;
    58.         //vertices = mesh.vertices.Length;
    59.         Vector3 right = rotation * Vector3.right;
    60.  
    61.         //Debug.Log (Vector3.right);
    62.         Vector3 up = rotation * Vector3.up;
    63.  
    64.  
    65.         up.Set (roadWidth, 0f, roadWidth);
    66.  
    67.  
    68.         if (Start.y > positive) {
    69.             right.Set (roadWidth, 0f,roadWidth);
    70.             left.Set (roadWidth, 0f,roadWidth);
    71.             vertices.Add(Start + -right);
    72.             vertices.Add(Start + left);
    73.         } else {
    74.             if (
    75.                 Start.y < negative) {
    76.                 right.Set (roadWidth, 0f,roadWidth);
    77.                 left.Set (roadWidth, 0f,roadWidth);
    78.                 vertices.Add(Start + right);
    79.                 vertices.Add(Start + -left);
    80.             }
    81.         }
    82.  
    83.         //Debug.Log (vertices.Count);
    84.  
    85.             //Debug.Log (Start);
    86.  
    87.             normals.Add (up);
    88.             normals.Add (up);
    89.        
    90.         int triIndex = 0;
    91.         for (int i = 0; i <= detailLvl; i++)
    92.         {
    93.             float t = (float)i / (float)detailLvl;
    94.             Vector3 End = GetPoint(t);
    95.             rotation = GetRotation(t);
    96.             left = rotation * Vector3.left;
    97.  
    98.             right = rotation * Vector3.right;
    99.  
    100.             //Debug.Log (rotation);
    101.             up = rotation * Vector3.up;
    102.             up.Set (roadWidth, 0f, roadWidth);
    103.             if (rotation.y > positive) {
    104.                 right.Set (roadWidth, 0f,roadWidth);
    105.                 left.Set (roadWidth, 0f,roadWidth);
    106.                 vertices.Add(End + -right);
    107.                 vertices.Add(End + left);
    108.             } else {
    109.                 if (End.y < negative) {
    110.                     right.Set (roadWidth, 0f,roadWidth);
    111.                     left.Set (roadWidth, 0f,roadWidth);
    112.                     vertices.Add(End + right);
    113.                     vertices.Add(End + -left);
    114.                 }
    115.             }
    116.  
    117.  
    118.                 normals.Add (up);
    119.                 normals.Add (up);
    120.            
    121.  
    122.             triangles.Add(triIndex);
    123.             triangles.Add(triIndex + 1);
    124.             triangles.Add(triIndex + 2);
    125.  
    126.             triangles.Add(triIndex + 2);
    127.             triangles.Add(triIndex + 1);
    128.             triangles.Add(triIndex + 3);
    129.  
    130.             triIndex += 2;
    131.        
    132.             Start = End;
    133.         }
    134.  
    135.         //Debug.Log (vertices.Count);
    136.         mesh.SetVertices(vertices);
    137.         mesh.SetNormals(normals);
    138.    
    139.         mesh.name = "PathPlane" + totalroads;
    140.         mesh.RecalculateNormals ();
    141.         mesh.RecalculateBounds ();
    142.         mesh.SetTriangles(triangles, 0);
    143.  
    144.         GetComponent<MeshFilter>().mesh = mesh;
    145.        
    146.     }
    147.     private Quaternion GetRotation(float t)
    148.     {
    149.         return Quaternion.LookRotation(GetDirection(t), Vector3.up);
    150.     }
    151.     public Vector3 GetPoint (float t) {
    152.         int i;
    153.  
    154.         Vector3[] points = BentSpline.GetComponent<BezierSpline>().points;
    155.         if (t >= 1f) {
    156.             t = 1f;
    157.             i = points.Length - 4;
    158.         }
    159.         else {
    160.             t = Mathf.Clamp01(t) * BentSpline.GetComponent<BezierSpline>().CurveCount;
    161.             i = (int)t;
    162.             t -= i;
    163.             i *= 3;
    164.         }
    165.  
    166.         return transform.TransformPoint(Bezier.GetPoint(points[i], points[i + 1], points[i + 2], points[i + 3], t));
    167.     }
    168.     //public Vector3 GetPoint(float t)
    169.     //{
    170. //        return Bezier.GetPoint(start.position, start.position + startDirection, end.position - endDirection, end.position, t);
    171.     //}
    172.  
    173.     public Vector3 GetVelocity (float t) {
    174.         int i;
    175.         Vector3[] points = BentSpline.GetComponent<BezierSpline>().points;
    176.         if (t >= 1f) {
    177.             t = 1f;
    178.             i = points.Length - 6;
    179.         }
    180.         else {
    181.             t = Mathf.Clamp01(t) * BentSpline.GetComponent<BezierSpline>().CurveCount;
    182.             i = (int)t;
    183.             t -= i;
    184.             i *= 3;
    185.         }
    186.         return transform.TransformPoint(Bezier.GetFirstDerivative(points[i], points[i + 1], points[i + 2], points[i + 3], t)) - transform.position;
    187.     }
    188.  
    189.     public Vector3 GetDirection (float t) {
    190.         return GetVelocity(t).normalized;
    191.     }
    192. }
    193.  
     
  2. ReggieBeRetro

    ReggieBeRetro

    Joined:
    Aug 30, 2015
    Posts:
    73
    Sorry let me make a small correction. the handlers end up int he right transform position but the mesh is off in left field.
     
  3. ReggieBeRetro

    ReggieBeRetro

    Joined:
    Aug 30, 2015
    Posts:
    73