Created
January 20, 2018 14:22
-
-
Save jiankaiwang/976591f56207868272fa41b14d1a6894 to your computer and use it in GitHub Desktop.
The C# script makes the camera move on the path desired in Unity3D.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* author : jiankaiwang | |
* description : The C# script makes the camera move on the path desired in Unity3D. | |
* platform : Unity | |
* date : 2018/01 | |
*/ | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
// struct for controling camera moving and rotating | |
public struct CamNavCtlPoint { | |
public Vector3 Position; | |
public Quaternion Rotation; | |
} | |
// struct recording the start and end indexes within the position | |
public struct PosArray { | |
public int start, end; | |
} | |
// moving unit for each dimensions | |
public struct PosIncreament { | |
public float X, Y, Z; | |
} | |
public class AutoCamera : MonoBehaviour { | |
// the step unit between two positions | |
public int step = 100; | |
public int initX = 0, initY = 0, initZ = 0; | |
// use stepIndex to check whether the step is reached | |
private int stepIndex = 1; | |
private float moveX, moveY, moveZ; | |
private int angleX, angleY, angleZ; | |
private List<CamNavCtlPoint> ControlPoints = new List<CamNavCtlPoint>(); | |
private PosArray posIndex = new PosArray(); | |
private PosIncreament posIncreament = new PosIncreament(); | |
// Use this for initialization | |
void Start() { | |
// defined location | |
ControlPoints.Add(new CamNavCtlPoint() { Position = new Vector3(initX, initY, initZ), Rotation = Quaternion.Euler(Vector3.zero) }); | |
ControlPoints.Add(new CamNavCtlPoint() { Position = new Vector3(10, 22, 10), Rotation = Quaternion.Euler(Vector3.zero) }); | |
ControlPoints.Add(new CamNavCtlPoint() { Position = new Vector3(20, 2, 10), Rotation = Quaternion.Euler(Vector3.zero) }); | |
// initialization | |
moveX = 0; | |
moveY = 0; | |
moveZ = 0; | |
posIndex.start = 0; | |
posIndex.end = 1; | |
posIncreament.X = 0.0f; | |
posIncreament.Y = 0.0f; | |
posIncreament.Z = 0.0f; | |
angleX = 0; | |
angleY = 0; | |
angleZ = 0; | |
calculateIncreamentUnit(); | |
} | |
// get the increament amount for each dimensions on moving the position to the next one | |
void calculateIncreamentUnit() { | |
int startPos = posIndex.start; | |
int endPos = posIndex.end; | |
posIncreament.X = (ControlPoints[endPos].Position[0] - ControlPoints[startPos].Position[0]) / (float)step; | |
posIncreament.Y = (ControlPoints[endPos].Position[1] - ControlPoints[startPos].Position[1]) / (float)step; | |
posIncreament.Z = (ControlPoints[endPos].Position[2] - ControlPoints[startPos].Position[2]) / (float)step; | |
} | |
// get the new pair of start and end position index | |
void indexMove() { | |
posIndex.start = (posIndex.start + 1) % ControlPoints.Count; | |
posIndex.end = (posIndex.start + 1) % ControlPoints.Count; | |
// re-calculate the step unit | |
calculateIncreamentUnit(); | |
} | |
// Update is called once per frame | |
void Update () { | |
if (stepIndex++ > step) { | |
indexMove(); | |
stepIndex = 1; | |
} | |
// -------------------------------------------------------------------- | |
// auto-move | |
// -------------------------------------------------------------------- | |
// X : straffe += speed * Time.deltaTime; | |
moveX = posIncreament.X; | |
// Y | |
moveY = posIncreament.Y; | |
// Z : translation += speed * Time.deltaTime; | |
moveZ = posIncreament.Z; | |
transform.Translate(moveX, moveY, moveZ); | |
// -------------------------------------------------------------------- | |
// auto-rotate | |
// localRotation would execute the final code | |
// -------------------------------------------------------------------- | |
// rotate on x-axis | |
//angleX = (angleX + 1) % 360; | |
//transform.localRotation = Quaternion.AngleAxis(angleX, Vector3.right); | |
// rotate on y-axis | |
//angleY = (angleY + 1) % 360; | |
//transform.localRotation = Quaternion.AngleAxis(angleY, Vector3.up); | |
// rotate on z-axis | |
//angleZ = (angleZ + 1) % 360; | |
//transform.localRotation = Quaternion.AngleAxis(angleZ, Vector3.forward); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment