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
1
Question by alexander11 · Aug 12, 2016 at 01:06 AM · c#3dgameobjectsspline

How do i Have GameObjects along a spline(Like Nodes)??

Hello i have been looking at Catmull Splines and i have been trying to create Gameobjects along the spline, while the Objects Face along the spline, to see what i mean see Pic1.

I can only create the spline which i learnt from here.

Does anyone know how to do this?

-Pic1. alt text

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

 using UnityEngine;
 using System.Collections.Generic;
 
 public class Spline : MonoBehaviour {
 
     public List<Transform> controlPointsList = new List<Transform>();
 
     public bool isLooping = true;
 
     void OnDrawGizmos()
     {
         for (int i = 0; i < controlPointsList.Count; i++)
         {
             //Cant draw between the endpoints
             //Neither do we need to draw from the second to the last endpoint
             //...if we are not making a looping line
             if ((i == 0 || i == controlPointsList.Count - 2 || i == controlPointsList.Count - 1) && !isLooping)
             {
                 continue;
             }
 
             DisplayCatmullRomSpline(i);
         }
         Gizmos.color = Color.white;
 
         //Draw a sphere at each control point
         for (int i = 0; i < controlPointsList.Count; i++)
         {
             Gizmos.DrawWireSphere(controlPointsList[i].position, 0.3f);
         }
     }
     Vector3 ReturnCatmullRom(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
     {
         Vector3 a = 0.5f * (2f * p1);
         Vector3 b = 0.5f * (p2 - p0);
         Vector3 c = 0.5f * (2f * p0 - 5f * p1 + 4f * p2 - p3);
         Vector3 d = 0.5f * (-p0 + 3f * p1 - 3f * p2 + p3);
 
         Vector3 pos = a + (b * t) + (c * t * t) + (d * t * t * t);
 
         return pos;
     }
 
 void DisplayCatmullRomSpline(int pos)
 {
     //Clamp to allow looping
     Vector3 p0 = controlPointsList[ClampListPos(pos - 1)].position;
     Vector3 p1 = controlPointsList[pos].position;
     Vector3 p2 = controlPointsList[ClampListPos(pos + 1)].position;
     Vector3 p3 = controlPointsList[ClampListPos(pos + 2)].position;
 
 
     //Just assign a tmp value to this
     Vector3 lastPos = Vector3.zero;
 
     //t is always between 0 and 1 and determines the resolution of the spline
     //0 is always at p1
     for (float t = 0; t < 1; t += 0.1f)
     {
         //Find the coordinates between the control points with a Catmull-Rom spline
         Vector3 newPos = ReturnCatmullRom(t, p0, p1, p2, p3);
 
         //Cant display anything the first iteration
         if (t == 0)
         {
             lastPos = newPos;
             continue;
         }
 
         Gizmos.DrawLine(lastPos, newPos);
         lastPos = newPos;
     }
 
     //Also draw the last line since it is always less than 1, so we will always miss it
     Gizmos.DrawLine(lastPos, p2);
 }
 
 
 //Clamp the list positions to allow looping
 //start over again when reaching the end or beginning
 int ClampListPos(int pos)
 {
     if (pos < 0)
     {
         pos = controlPointsList.Count - 1;
     }
 
     if (pos > controlPointsList.Count)
     {
         pos = 1;
     }
     else if (pos > controlPointsList.Count - 1)
     {
         pos = 0;
     }
 
     return pos;
 }
 }
 

@Bunny83 @Fattie

capture15.png (42.7 kB)
Comment
Add comment · Show 1
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
avatar image Fattie · Aug 12, 2016 at 10:36 AM 0
Share

Honestly I would just use one of the packages on the asset store which does this - Spline Pro or the like

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Aug 12, 2016 at 05:53 PM

I'm not quite sure what you actually have problems with. If you just don't know how to get the tangent of the spline there are two ways:

  • get two points on the spline which are close to each other ("t" and "t + 0.001f") and subtract the first from the second. This gives you a very good approximation of the tangent at the point "t"

  • Calculate the derivative of the catmul rom spline equation and use that to get the exact tangent for a given point.

The derivative would be:

 // spline:
 // d*t*t*t + c*t*t + b*t + a
 
 // tangent:
 // 3*d*t*t + 2*c*t + b
 
 Vector3 ReturnCatmullRomTangent(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
 {
     Vector3 b = 0.5f * (p2 - p0);
     Vector3 c = (2f * p0 - 5f * p1 + 4f * p2 - p3);
     Vector3 d = 1.5f * (-p0 + 3f * p1 - 3f * p2 + p3);

     Vector3 tangent = b + c * t + d * t * t;
     return tangent.normalized;
 }

This calculates the tangent for the given set of control points and the position "t" on that section of the spline. This tangent can be used with Quaternion.LookRotation to orient your objects so their z-axis points along the spline.

Here's the same Gizmo code but additionally draw the tangents in yellow

You said you "learnt" that code from the given link but to me it looks like you just copied it from there. Are you sure you actually learnt anything from there? Do you understand the code? Even if you don't understand the formula, do you understand what each of those methods is good for and how you should use them?


edit
If someone is interested how this continues, we have a follow up quesition in the help room.

Comment
Add comment · Show 4 · Share
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
avatar image alexander11 · Aug 12, 2016 at 10:03 PM 0
Share

I kind of understand the forumla, and the other stuff i know. Yes i did copied the code from the Website but i have my own spline, i just thought loading it from the website would make it easier for the people who could answer the question, if they knew something i dont. Thanks again just what i was looking for.

avatar image alexander11 · Aug 14, 2016 at 10:59 PM 0
Share

I am still having trouble with the tangents can you please visit here i need your help @Bunny83 .

avatar image Bunny83 alexander11 · Aug 15, 2016 at 02:25 AM 0
Share

Uhm, i'm not sure why you posted a new question since you haven't included my tangent method in your other question. All i changed is this:

I replaced those two lines:

 Gizmos.DrawLine(lastPos, newPos);
 lastPos = newPos;

which those:

 Gizmos.color = Color.white;
 Gizmos.DrawLine(lastPos, newPos);
 
 Gizmos.color = Color.yellow;
 var tan = ReturnCatmullRomTangent(t, p0, p1, p2, p3);
 Gizmos.DrawLine(newPos, newPos + tan*3);
 
 lastPos = newPos;
avatar image alexander11 Bunny83 · Aug 15, 2016 at 05:12 AM 0
Share

Thank you. I just tried to get your attention for the past 2 days. i'll remove the other threads, and i miss read the ReturnCatmullRomTangent thinking it was ReturnCatmullRom.

And i accidentally posted the same thread twice.

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

205 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

Related Questions

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

How do i make a Linear Spline?? 1 Answer

3D arrangement of collection of gameobjects 0 Answers

Adding segments in missing spots 1 Answer

Multiple Cars not working 1 Answer


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