I am trying to make an object follow a path made up of nodes like the image below.
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:
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: Cyberspace Progress Video 1 - YouTube
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.