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 CyberChroma · Sep 04, 2018 at 12:46 PM · script.navigationpathfollowing

Moving an object along a circular path

I am trying to make an object follow a path made up of nodes like the image below.


alt text


There have been many others questions similar to this, but my problem is a bit different. I can make an object move from one point to the next, then the next, and so on, but where things become tricky is the turns. When going from one node to the next, I want the object to smoothly turn to face the forward direction of the next node (like between nodes (1) and (2) above or (3) and (4)). The object should always face the direction it is moving.


If possible, I would also like this to work if the nodes were moving during gameplay (which I can also already do).


EDIT: There were some misunderstandings with my question, so I will elaborate more here instead of buried in a reply.


That green path was drawn in photoshop. All I have in Unity in terms of positions are those blue nodes. Currently, I can make a path that looks like this by moving straight from one point to the next:


alt text


What I want to achieve is the circular movements from node (1) to node (2) and node (3) to node (4). I also want the forward direction of each node to determine which way the object going along the path (imagine a spaceship, I guess) should be turning to face.


The game is also 3d, so the path could go in all directions, on any axis, and aren't locked to a plane. The flat pictures were for simplicity. Preferably, I would like to not use physics nor the navmesh system, but rather manipulating transforms. I also can't bake an animation since the path can change if one node is moving back and forth or it there is a split path.


One idea I did have is that I could make the object move forward relative to itself, and then turn towards the next node as it's moving, but I am unsure of how I would calculate that rotation speed so that it goes through the next node and not past it.


EDIT 2: Here is my code currently. The NodeInfo class is a script on every node that I'm using for things beyond the scope of this question, like changing speeds and looping to different parts of the path. The turning is working fine, but the path is still just linear. I've tried many things like using forces, lerping, calculating new in-between points, a bezier curve asset from the asset store, and now using percentages.


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MoveF : MonoBehaviour {
 
     public NodeInfo[] nodes;
 
     private int currentNode = 1;
     private int lastNode = 0;
     private float perc = 0;
 
     void Update () {
         perc += nodes[currentNode].speed * Time.deltaTime;
         transform.position = Vector3.Lerp (nodes[lastNode].transform.position, nodes[currentNode].transform.position, perc);
         transform.rotation = Quaternion.Lerp (nodes[lastNode].transform.rotation, nodes[currentNode].transform.rotation, perc);
         if (Vector3.Distance(transform.position, nodes[currentNode].transform.position) <= 0.1f && Quaternion.Angle(transform.rotation, nodes[currentNode].transform.rotation) <= 1f)
         {
             lastNode = currentNode;
             if (nodes[currentNode].nodeType == NodeInfo.NodeType.normal)
             {
                 currentNode++;
                 if (currentNode >= nodes.Length)
                 {
                     enabled = false;
                 }
             }
             else if (nodes[currentNode].nodeType == NodeInfo.NodeType.loop)
             {
                 currentNode = nodes[currentNode].loopTo;
             }
             perc = 0;
         }
     }
 }



EDIT 3: Here is a video of my progress so far with this and should give a better idea of what I want to achieve: https://www.youtube.com/watch?v=cezdf_hCieE&feature=youtu.be The plan is that enemies will come in waves like a combination of Galaga or Space Invaders as you move through an environment going from point a to point b. It works so far but the turns are very snappy because there is no roundedness in the turns.

node-path.jpg (50.4 kB)
node-path.jpg (43.5 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 misher · Sep 04, 2018 at 12:53 PM 0
Share

Post your code first, what did you try? The question must be more precise than "give me a ready script".

3 Replies

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

Answer by CyberChroma · Sep 06, 2018 at 04:52 PM

I was finally able to get this to work by manipulating the cosine law. For anyone that comes across this, I will paste my working code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MoveF : MonoBehaviour {
 
     public NodeInfo[] nodes;
 
     private int currentNode = 1;
     private int lastNode = 0;
     private float perc = 0;
     private Vector3 midPoint;
     private Vector3 m1;
     private Vector3 m2;
 
     void Update () {
         perc += (1 / Vector3.Distance(nodes[lastNode].transform.position, nodes[currentNode].transform.position)) * nodes[currentNode].speed * Time.deltaTime;
         if (Vector3.Angle(nodes[lastNode].transform.forward, nodes[currentNode].transform.position - nodes[lastNode].transform.position) > 0)
         {
             midPoint = nodes[lastNode].transform.position + nodes[lastNode].transform.forward * Vector3.Distance(nodes[lastNode].transform.position, nodes[currentNode].transform.position) / Mathf.Sqrt(2 * (1 - Mathf.Cos((180 - 2 * Vector3.Angle(nodes[lastNode].transform.forward, nodes[currentNode].transform.position - nodes[lastNode].transform.position)) * Mathf.Deg2Rad)));
         }
         else
         {
             midPoint = (nodes[lastNode].transform.position + nodes[currentNode].transform.position) / 2;
         }
         m1 = Vector3.Lerp(nodes[lastNode].transform.position, midPoint, perc);
         m2 = Vector3.Lerp(midPoint, nodes[currentNode].transform.position, perc);
         transform.position = Vector3.Lerp (m1, m2, perc);
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Slerp(nodes[lastNode].transform.rotation, nodes[currentNode].transform.rotation, perc), 0.5f);
         if (Vector3.Distance(transform.position, nodes[currentNode].transform.position) <= 0.1f && Quaternion.Angle(transform.rotation, nodes[currentNode].transform.rotation) <= 1f)
         {
             lastNode = currentNode;
             if (nodes[currentNode].nodeType == NodeInfo.NodeType.normal)
             {
                 currentNode++;
                 if (currentNode >= nodes.Length)
                 {
                     enabled = false;
                 }
             }
             else if (nodes[currentNode].nodeType == NodeInfo.NodeType.loop)
             {
                 currentNode = nodes[currentNode].loopTo;
             }
             perc = 0;
         }
     }
 }

Comment
Add comment · Show 1 · 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 mansoor090 · Mar 28, 2019 at 07:20 AM 0
Share

can you share your nodeinfo class ?

avatar image
1

Answer by eses · Sep 04, 2018 at 01:38 PM

@Superspeedy

Your question is bit mixed; heading is "Making an object follow a path" but you say you already have this working.

By "galiga style game" do you mean classic game called "Galaga" perhaps?

Then you say " but where things become tricky is the turns." - so the turning is the problem?

What @tormentoarmagedoom mentioned could work - players don't know if objects are moving on invisible plane or not...

Maybe look into using some spline asset from asset store, or some tweening kit? Some of those have path / spline support.

Facing towards path next point problem - Get current and next point or point on path little bit forward in time, then calculate a direction vector from these. Use this as your transform forward for the moving object.

Comment
Add comment · Show 1 · 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 CyberChroma · Sep 04, 2018 at 05:33 PM 0
Share

Yes, I meant Galaga. I'll update the question to give more detail as to what I mean.

avatar image
0

Answer by tormentoarmagedoom · Sep 04, 2018 at 12:47 PM

Good day.

Use the nav mesh system, and configure the angular acceleration of the nav mesh agent to a low value.

Bye.

Comment
Add comment · Show 1 · 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 CyberChroma · Sep 04, 2018 at 01:11 PM 0
Share

As far as I know, a nav mesh can only be made on surfaces. I need something that will work in the air. I am making a galiga style game. The game area consisting of the player, camera, boundaries, ect. will be moving along this path to be traversing an environment.

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

98 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

Related Questions

Movement and animation 1 Answer

Prefab Position 2 Answers

Object slows down when moving to a NavPoint, speed is not constant 1 Answer

help with not working AI system 2 Answers

How to lock an int as a playerPref? 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