- Home /
Question by
statmatics · May 29, 2015 at 05:33 AM ·
global variablesynchronize
Camera following a moving object
I am trying to follow a moving space ship with the camera using global variables to pass the ship's position to the camera. It works, but the display jitters. Are there better ways? (Once movement works smoothly I want to mess with the ship's rotation, but leave the camera un-rotated.)
public class Ship : MonoBehaviour {
// global variables for passing ship position to camera
public static float shipX;
public static float shipY;
// Initialization
void Start () {
}
// Once per frame
void Update () {
transform.Translate(5f * Time.deltaTime, 0f, 0f);
// globals for camera
shipX = transform.localPosition.x;
shipY = transform.localPosition.y;
}
}
public class Cam : MonoBehaviour {
private Vector3 newPosition;
// Initialization
void Start () {
}
// Once per frame
void Update () {
// follow ship with camera
newPosition = transform.position;
newPosition.x = Ship.shipX;
newPosition.y = Ship.shipY;
newPosition.z = -30f;
transform.position = newPosition;
}
}
Comment
Your answer
Follow this Question
Related Questions
Synchronize Cannonballs(networking) 2 Answers
Accessing global variable from java in c# 2 Answers
Unet Sync Particle System 1 Answer
Static variable for accessing global object? 3 Answers
Dealing damage over a network 1 Answer