- Home /
Alternatives to iTweenPath for creating path for camera
Continuing on from comments below. Each section has a script attached to it which does 2 things (called ThisSectionInfoManager).
Finds its Endpoint
Contains a public Transform array storing the path points positions, etc.The array is called 'RailMarkersList')
The code for this is as follows:
public class ThisSectionInfoManager : MonoBehaviour
{
public Transform EndPoint;
public Transform[] RailMarkersList;
void Start ()
{
EndPoint = transform.Find("sectionend");
}
void Update ()
{
}
}
------
3 sections are maintained at all times with the camera starting in the first [0]. Once it approaches the start-point of the following section [1], a new section is generated and the first removed. In this way, the camera is always just entering the first section.
Now, a script called SectionManager generates and aligns the new section at the end. At the same time, the script that deals with the path following (CameraPathFollow) accesses the SectionManager script to find out what the first section of the array is (CurrentlyLaidSectionList[0]). It then goes on to access that section's script mentioned earlier (ThisSectionInfoManager) to copy its path points array (called RailMarkersList) into an array here (called Section_1_path, for instance). it then goes ahead and follows the path as we talked about below.
The CamerPathFollow script is as follows;
public class CameraPathFollow : MonoBehaviour
{
// Tranform arrays to hold the empty objects that form the path of the camera.
// Points dragged in from Inspector - One array for each section.
public Transform[] Section_1_path;
public Transform[] Section_2_path;
public Transform[] Section_3_path;
public Transform[] Section_4_path;
public Transform[] Section_5_path;
public Transform[] Section_6_path;
//
float t = 0;
// Variable to hold the tag of the section that the camera is in front of [0]
private Transform sectionToFollow;
// Reference to the Parking Lot object
private GameObject parkingLotObj;
//private SmoothQuaternion sr = new Quaternion();
private SmoothQuaternion sr = new SmoothQuaternion();
public float CameraSpeed;
void Start ()
{
// Gain access to the Parking Lot object
parkingLotObj = GameObject.Find("Parking Lot");
sr = transform.rotation;
sr.Duration = 0.5f;
}
void Update()
{
// Set the nextSection variable to hold the tag of the section in the second element [1] of the
// CurrentlyLaidSectionList List - this is the one that the camera is always about to enter
sectionToFollow = parkingLotObj.GetComponent<SectionManager>().CurrentlyLaidSectionList[0];
// use swtih statement to decide which Transform array above to use for the path of the camera,
// depending on which section is in CurrentlyLaidSectionList[1] at the time.
switch (sectionToFollow.tag)
{
case "Section_1":
{
Quaternion q = new Quaternion();
// Copy the RailMarkersList into our array here so that the camera has the positions of the
// markers after the section has been positioned
Section_1_path = sectionToFollow.GetComponent<ThisSectionInfoManager>().RailMarkersList;
// transform.position = Spline.MoveOnPath(Section_6_path, transform.position, ref t, ref q, 30f);
transform.position = Spline.MoveOnPath(Section_1_path, transform.position, ref t, ref q, CameraSpeed, 100, EasingType.Sine, true, true);
sr.Value = q;
transform.rotation = sr;
// Debug.Log("Entering Section 1");
break;
}
case "Section_2":
{
Quaternion q = new Quaternion();
// Copy the RailMarkersList into our array here so that the camera has the positions of the
// markers after the section has been positioned
Section_2_path = sectionToFollow.GetComponent<ThisSectionInfoManager>().RailMarkersList;
// transform.position = Spline.MoveOnPath(Section_6_path, transform.position, ref t, ref q, 30f);
transform.position = Spline.MoveOnPath(Section_2_path, transform.position, ref t, ref q, CameraSpeed, 100, EasingType.Sine, true, true);
sr.Value = q;
transform.rotation = sr;
// Debug.Log("Entering Section 2");
break;
}
case "Section_3":
{
Quaternion q = new Quaternion();
// Copy the RailMarkersList into our array here so that the camera has the positions of the
// markers after the section has been positioned
Section_3_path = sectionToFollow.GetComponent<ThisSectionInfoManager>().RailMarkersList;
// transform.position = Spline.MoveOnPath(Section_6_path, transform.position, ref t, ref q, 30f);
transform.position = Spline.MoveOnPath(Section_3_path, transform.position, ref t, ref q, CameraSpeed, 100, EasingType.Sine, true, true);
sr.Value = q;
transform.rotation = sr;
// Debug.Log("Entering Section 3");
break;
}
case "Section_4":
{
Quaternion q = new Quaternion();
// Copy the RailMarkersList into our array here so that the camera has the positions of the
// markers after the section has been positioned
Section_4_path = sectionToFollow.GetComponent<ThisSectionInfoManager>().RailMarkersList;
// transform.position = Spline.MoveOnPath(Section_6_path, transform.position, ref t, ref q, 30f);
transform.position = Spline.MoveOnPath(Section_4_path, transform.position, ref t, ref q, CameraSpeed, 100, EasingType.Sine, true, true);
sr.Value = q;
transform.rotation = sr;
// Debug.Log("Entering Section 4");
break;
}
case "Section_5":
{
Quaternion q = new Quaternion();
// Copy the RailMarkersList into our array here so that the camera has the positions of the
// markers after the section has been positioned
Section_5_path = sectionToFollow.GetComponent<ThisSectionInfoManager>().RailMarkersList;
// transform.position = Spline.MoveOnPath(Section_6_path, transform.position, ref t, ref q, 30f);
transform.position = Spline.MoveOnPath(Section_5_path, transform.position, ref t, ref q, CameraSpeed, 100, EasingType.Sine, true, true);
sr.Value = q;
transform.rotation = sr;
// Copy the RailMarkersList into our array here so that the camera has the positions of the
// markers after the section has been positioned
// Section_5_path = sectionToFollow.GetComponent<ThisSectionInfoManager>().RailMarkersList;
// transform.position = Spline.MoveOnPath(Section_5_path, transform.position, ref t, CameraSpeed);
// Debug.Log("Entering Section 5");
break;
}
case "Section_6":
{
Quaternion q = new Quaternion();
// Copy the RailMarkersList into our array here so that the camera has the positions of the
// markers after the section has been positioned
Section_6_path = sectionToFollow.GetComponent<ThisSectionInfoManager>().RailMarkersList;
// transform.position = Spline.MoveOnPath(Section_6_path, transform.position, ref t, ref q, 30f);
transform.position = Spline.MoveOnPath(Section_6_path, transform.position, ref t, ref q, CameraSpeed, 100, EasingType.Sine, true, true);
sr.Value = q;
transform.rotation = sr;
// transform.position = Spline.MoveOnPath(Section_6_path, transform.position, ref t, 300f);
//transform.rotation = Spline.RotationBetween(Section_6_path, );
// Debug.Log("Entering Section 6");
break;
}
default:
return;
}// ends switch
}
Answer by whydoidoit · Jun 27, 2012 at 12:22 AM
You can use my Unity Path Following code and use MoveOnPath. It takes an array of transforms so they can move.
Also this is a useful script to add to empty game objects that act as path points:
using UnityEngine;
using System.Collections;
public class PathPosition : $$anonymous$$onoBehaviour {
public int position = -1;
public void OnDrawGizmos()
{
//Gizmos.DrawIcon(transform.position, "gold_pin-2.png");
Gizmos.color = Color.yellow;
Gizmos.DrawLine(transform.position, transform.position + transform.forward/2);
Gizmos.DrawSphere(transform.position + transform.forward/2, 0.02f);
if(position != -1) TextGizmo.Draw(transform.position, string.Format("position = {0}", position));
}
}
Sounds good, I shall give it a shot!
For the above script I'm getting the following error;
Error CS0103: The name `TextGizmo' does not exist in the current context
Thanks
Oh sorry just delete that line, forgot that was one of $$anonymous$$e :)
No worries. Got that working, makes life a lot easier.
Also, would I need to use the version of $$anonymous$$oveToPath() that returns a rotation for this?
When placing the empty objects in the tubes for the prefabs/slots in Inspector, do I also need to rotate each of them so that their Z-axis is facing in the tube's forward direction?
Edit: O$$anonymous$$, so it's now following the path but not looking in the right direction. Have tried to download the Smoothing.cs file to use SmoothQuaternion as in your example from link above but its not working?
I don't suppose you could steer me in the right direction WRT to the rotation?
Thanks :)
So there's a version of move on path that returns the desired direction I think. What's up with SmoothQuaternion? What effect are you seeing?
Does this code not work:
#pragma strict
var pathPoints : Transform[];
var t : float;
var sr : SmoothQuaternion;
function Start() {
sr = transform.rotation;
sr.Duration = 0.5f;
}
function Update () {
var q : Quaternion;
transform.position = Spline.$$anonymous$$oveOnPath(pathPoints, transform.position, t, q, 0.5f, 100, EasingType.Sine, true, true);
sr.Value = q;
transform.rotation = sr;
}
Answer by jasperstocker · Feb 21, 2014 at 03:44 PM
Might I suggest Camera Path? It was designed specifically to animate cameras on a path with a very simple interface.