Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by alexander11 · Sep 07, 2016 at 03:16 AM · c#unity 53dsplineincorrect

Problem with curved Cornered Spline that i need help with

Hello i have been trying to do this in Pic1, but i cant do it, iv'e been trying different ways but it is either deformed or just gets errors, the closest i could get is in Pic2, but its not what i want, would anyone know how to fix this???

(visit here more about the positioning on the blue dots)

  • -Pic1.(What i am trying to do) alt text NOTE: segments = segCount


  • -Pic2.(The results i am getting) alt text


    And here is the code so you know what i have done.

    using UnityEngine; using System.Collections; using System.Collections.Generic;

    public class Spline : MonoBehaviour {

       [System.Serializable]
         public class SplinePoint
         {
             public Transform t;
             public Transform cpAhead;
             public Transform cpBehind;
             public int seg;
             public SplinePoint(Transform thetrans, Transform a, Transform b, int s)
             {
                 t = thetrans;
                 cpAhead = a;
                 cpBehind = b;
                 seg = s;
             }
         }
         public List<Transform> controlPointsList;
         public List<SplinePoint> splinePoints;
         public Transform segment;
         public int segCount = 2;
         public int curveSegments = 5;
         public float updateRate = 60f;
         public bool setup = false;
         public bool running = false;
         public float distance;
     
         void Start()
         {
             splinePoints = new List<SplinePoint>();
             CreateObjects(segment,distance);
         }
     
         public void CreateObjects(Transform original, float distance)
         {
             for (int i = 0; i < controlPointsList.Count - 1; i++)
             {
                 for (int j = 0; j < segCount; j++)
                 {
                     var a = Instantiate(original, Vector3.zero, Quaternion.identity) as Transform;
                     splinePoints.Add(new SplinePoint(a.transform, controlPointsList[i + 1], controlPointsList[i], j));
                 }
             }
             setup = true;
         }
         void Update()
         {
             if (setup && !running)
             {
                 UpdateSplinePos();
             }
         }
         //-
         void UpdateSplinePos()
         {
             for (int i = 0; i < splinePoints.Count; i++)
             {
                 SetPosition(splinePoints[i]);
             }
         }
         SplinePoint GetSplinePoint(int s)
         {
             SplinePoint sp = null;
             if (splinePoints.Count > 0)
             {
                 for (int i = 0; i < splinePoints.Count; i++)
                 {
                     if (splinePoints[i].seg == s)
                     {
                         sp = splinePoints[i];
                         break;
                     }
                 }
             }
             return sp;
         }
         void OnDrawGizmos()
         {
             if (!setup)
             {
                 return;
             }
             Gizmos.color = Color.white;
             for (int i = 0; i < controlPointsList.Count; i++)
             {
                 Gizmos.DrawWireSphere(controlPointsList[i].position, 0.3f);
             }
             for (int i = 0; i < controlPointsList.Count - 1; i++)
             {
                 Gizmos.DrawLine(controlPointsList[i].position, controlPointsList[i + 1].position);
             }
     
             //Get a list of associated nodes relative to control point. We want the last of the current control point and the first of the next control point
             for (int i = 0; i < controlPointsList.Count - 1; i++)
             {
                 if (i == controlPointsList.Count - 2)
                 {
                     break;
                 }
     
                 //retrieve the 3 points of the curve
                 List<Vector3> curvepoints = SplineCurve(controlPointsList[i], controlPointsList[i + 1]);
     
                 Vector3 p0 = curvepoints[0];
                 Vector3 p1 = curvepoints[1];
                 Vector3 p2 = curvepoints[2];
     
                 //draw a line between the start of the curve and the end of the curve for reference
     
                 //Gizmos.color = Color.red;
                 // Gizmos.DrawLine(p0, p2);
     
                 Vector3 lastPos = Vector3.zero;
     
                 Gizmos.color = Color.blue;
                 for (int n = 0; n < curveSegments + 1; n++)
                 {
                     float t = (float)n / curveSegments;
                     Vector3 newPos = GetPoint(p0, p1, p2, t);
     
                     if (t == 0)
                     {
                         lastPos = newPos;
                         continue;
                     }
     
                     Gizmos.DrawLine(lastPos, newPos);
     
                     Gizmos.color = Color.yellow;
                     var tan = GetTangent(p0, p1, p2, t);
                     //Gizmos.DrawLine(newPos, newPos + tan * 0.5f);
     
                     Gizmos.DrawSphere(newPos, 0.3f);
     
                     lastPos = newPos;
                 }
     
             }
         }
     
         List<SplinePoint> NodesAssociated(Transform behindNode)
         {
             List<SplinePoint> p = new List<SplinePoint>();
             for (int i = 0; i < splinePoints.Count; i++)
             {
                 if (splinePoints[i].cpBehind == behindNode)
                 {
                     p.Add(splinePoints[i]);
                 }
             }
             return p;
         }
     
     
     
         List<Vector3> SplineCurve(Transform current, Transform ahead)
         {
             List<Vector3> curvePoints = new List<Vector3>();
     
             //Find the first point in the curve
             for (int i = 0; i < splinePoints.Count; i++)
             {
                 //we need the nodes associated with the current transform
                 //since we store the segment value in the spline point, all we need is the segment that has the max segment count stored
                 if (splinePoints[i].cpBehind == current && splinePoints[i].seg == segCount - 1)
                 {
                     curvePoints.Add(splinePoints[i].t.position);
                     break;
                 }
     
             }
     
             //the second point in the curve is the next control point
             curvePoints.Add(ahead.position);
     
             //Find the last point in the curve
             for (int i = 0; i < splinePoints.Count; i++)
             {
                 //we need the nodes associated with the ahead transform
                 //since we store the segment value in the spline point, all we need is the segment that has 0 as its seg
                 if (splinePoints[i].cpBehind == ahead && splinePoints[i].seg == 0)
                 {
                     curvePoints.Add(splinePoints[i].t.position);
                     break;
                 }
     
             }
     
             return curvePoints;
         }
         //-
         void SetPosition(SplinePoint sp)
         {
             //float dist = Vector3.Distance(sp.cpAhead.position, sp.cpBehind.position);
             //float step = dist / (segCount + 1);
             //Vector3 dir = (sp.cpAhead.position - sp.cpBehind.position).normalized;
             //sp.t.position = sp.cpBehind.position + ((dir * step) * (sp.seg + 1));
             //sp.t.rotation = Quaternion.LookRotation(dir);
     
             for (int i = 0; i < controlPointsList.Count - 1; i++)
             {
                 float xDiff = controlPointsList[i + 1].position.x - controlPointsList[i].position.x;
                 float yDiff = controlPointsList[i + 1].position.y - controlPointsList[i].position.y;
                 float zDiff = controlPointsList[i + 1].position.z - controlPointsList[i].position.z;
     
                 float dist = Mathf.Sqrt(xDiff * xDiff + yDiff * yDiff + zDiff * zDiff);
     
                 if (dist < segCount * distance)
                     distance = dist / segCount;
     
                 float step = distance / dist;
                 Vector3 oneDistance = new Vector3();
     
                 oneDistance.x = xDiff * step;
                 oneDistance.y = yDiff * step;
                 oneDistance.z = zDiff * step;
     
                 Vector3 dir = (sp.cpAhead.position - sp.cpBehind.position).normalized;
                 sp.t.position = sp.cpBehind.position + ((dir * step) * (sp.seg + 1));
                 sp.t.rotation = Quaternion.LookRotation(dir);
             }
     
         }
     
         public static Vector3 GetPoint(Vector3 p0, Vector3 p1, Vector3 p2, float t)
         {
             t = Mathf.Clamp01(t);
             float oneMinusT = 1f - t;
             return
                 oneMinusT * oneMinusT * p0 +
                 2f * oneMinusT * t * p1 +
                 t * t * p2;
         }
     
         public static Vector3 GetTangent(Vector3 p0, Vector3 p1, Vector3 p2, float t)
         {
             return
                 2f * (1f - t) * (p1 - p0) +
                 2f * t * (p2 - p1);
         }
     
     
     }
     
    
    

@b1gry4n

capture69.png (27.8 kB)
capture70.png (40.9 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

235 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i do this type of spline?? 1 Answer

How do i make a Linear Spline?? 1 Answer

Adding segments in missing spots 1 Answer

How do i create points along a spline??? 0 Answers

What is Matrix4x4? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges