- Home /
Question by
UnitySanta · Feb 04, 2015 at 01:49 PM ·
camerasmoothorbit
Camera setting
Hi, Right now, I have a planet which has a sphere orbit it. I have set the camera also orbiting the planet too. I want my camera follows behind the sphere and see the sphere and the planet as background. Below is my code, and the effect is unstable and sometimes the screen will shake. Can somebody give me a better script that the camera will look smoother to the sphere? Thanks!!
public class CameraControl : MonoBehaviour { public Transform center; void Start () { transform.LookAt (center); } void Update () { transform.LookAt (center); } }
Comment
found this. may help
public class SmoothFollow : $$anonymous$$onoBehaviour {
public Transform target; // The target we are following
public float distance; // The distance from the target along its Z axis
public float height; // the height we want the camera to be above the target
public float positionDamping; // how quickly we should get to the target position
public float rotationDamping; // how quickly we should get to the target rotation
Rigidbody rBody;
void Awake() {
rBody = GetComponent<Rigidbody>();
}
// Use this for public variable initialization
public void Reset() {
distance = 3;
height = 1;
positionDamping = 6;
rotationDamping = 60;
}
// LateUpdate is called once per frame
public void FixedUpdate () {
ensureReferencesAreIntact();
#region Get Transform $$anonymous$$anipulation
// The desired position
Vector3 targetPosition = target.position + target.up * height - target.forward * distance;
// The desired rotation
Quaternion targetRotation = Quaternion.LookRotation(target.position-transform.position, target.up);
#endregion
#region $$anonymous$$anipulate Transform
rBody.position = Vector3.$$anonymous$$oveTowards(rBody.position, targetPosition, positionDamping * Time.deltaTime);
rBody.rotation = Quaternion.RotateTowards(rBody.rotation, targetRotation, rotationDamping * Time.deltaTime);
#endregion
}
// Checks to make sure all required references still exist and disables the script if not
private void ensureReferencesAreIntact() {
if (target == null) {
Debug.LogError("No target is set in the SmoothFollow Script attached to " + name);
this.enabled = false;
}
}
}
Your answer