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 02, 2016 at 11:17 PM · c#unity 5transform3dupdate

How do i Update a Transforms Position, and how do i have objects face along a spline??

Hello i am having two issues, that i need help with.

(1). How do i update a Transfrom position when i move it like this in Pic1(Each time i try to do this it'll either spawn a whole bunch of objects then crash or not work)??

-Pic1 alt text


(2). And also i have been trying to do this for a long time how do i face objects in the z axis along a spline like this in Pic2??

-Pic2 alt text


Here is my 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 int segCount = 2;
     public Transform segment;
 
     Vector3 position;
 
     void Start()
     {
         duplicateObject(segment, segCount);
     }
 
     public void duplicateObject(Transform original, int seg)
     {
         seg++;
         for (int i = 0; i < controlPointsList.Count - 1; i++)
         {
             for (int j = 1; j < seg; j++)
             {
                 position = controlPointsList[i].position + j * (controlPointsList[i + 1].position - controlPointsList[i].position) / seg;
                 var a = Instantiate(original, position, Quaternion.identity) as Transform;
                 Vector3 rotation = controlPointsList[i].position + j * (controlPointsList[i + 1].position - controlPointsList[i].position);
                 a.rotation = Quaternion.LookRotation(rotation);
             }
         }
     }
     void OnDrawGizmos()
     {
         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);
         }
     }
 
 
 }

capture45.png (86.4 kB)
capture47.png (67.1 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

1 Reply

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

Answer by b1gry4n · Sep 03, 2016 at 02:16 AM

I went through and made a script that will do what you want, but since I dont know how you are creating your spline (i am guessing you are using transforms judging by the gizmos, so it isnt really a spline at the moment) I am just using regular Transforms for control points. When you get to making the spline, youre going to have to change how you are positioning these objects along it. The script below works as a solution to the question you asked with the given information. This tutorial gives insight into how placing objects along a spline is done: http://catlikecoding.com/unity/tutorials/curves-and-splines/

alt text

And after moving the control nodes around in runtime i get this: alt text

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class SplineTest : MonoBehaviour {
 
  
     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 GameObject segment;
     public int segCount = 2;
     public float updateRate = 0.25f;
     public bool setup = false;
     public bool running = false;
 
     void Start()
     {
         splinePoints = new List<SplinePoint>();
         CreateObjects(segment);
     }
 
     public void CreateObjects(GameObject original)
     {
         for (int i = 0; i < controlPointsList.Count - 1; i++)
         {
             for (int j = 0; j < segCount; j++)
             {
                 GameObject a = (GameObject)Instantiate(original, Vector3.zero, Quaternion.identity);
                 splinePoints.Add(new SplinePoint(a.transform, controlPointsList[i + 1], controlPointsList[i], j));
             }
         }
         setup = true;
     }
     void Update()
     {
         if (setup && !running)
         {
             running = true;
             InvokeRepeating("UpdateSplinePos", 0, updateRate);
         }
     }
     void UpdateSplinePos()
     {
         for (int i = 0; i < splinePoints.Count; i++)
         {
             SetPosition(splinePoints[i]);
         }
     }       
     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);
     }
 }

a.jpg (26.1 kB)
b.jpg (27.4 kB)
Comment
Add comment · Show 8 · 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 · Sep 03, 2016 at 02:53 AM 0
Share

Thanks @b1gry4n for the help Problem2 is now solved now for Problem1, i did try what you did with the update thing and i don't know how to do it correctly, i map the properties in another void(UpdateObjects) then put them in Update() but the duplicateObject cant read the position which i calculated in UpdateObjects(), would you know how to do this?

here is my sh*tty code :D

 Vector3 position;
 
     void Start()
     {
         duplicateObject(segment, segCount);
     }
 
     public void duplicateObject(Transform original, int seg)
     {
         seg++;
         for (int i = 0; i < controlPointsList.Count - 1; i++)
         {
             for (int j = 1; j < seg; j++)
             {
                 var a = Instantiate(original, position, Quaternion.identity) as Transform;
                 Vector3 dir = controlPointsList[i].position - controlPointsList[i+1].position;
                 a.rotation = Quaternion.LookRotation(dir);
             }
         }
     }
     void Update()
     {
         UpdateObjects();
     }
 
     void UpdateObjects()
     {
         for (int i = 0; i < controlPointsList.Count - 1; i++)
         {
             for (int j = 1; j < segCount; j++)
             {
                 position = controlPointsList[i].position + j * (controlPointsList[i + 1].position - controlPointsList[i].position) / segCount;
             }
         }
     }

avatar image b1gry4n alexander11 · Sep 03, 2016 at 03:09 AM 0
Share

Check the edited post, I gave an example of a working solution

avatar image alexander11 b1gry4n · Sep 03, 2016 at 03:19 AM 0
Share

So does the UpdateRate optimize the script??

Show more comments
Show more comments

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 fix Error CS0029? 1 Answer

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

How to move 3D character with acceleration and deceleration and face in direction of movement 0 Answers

How can I rotate my player with a Joystick? 0 Answers

Generate x,y,z coordinate for vehicles path 0 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