- Home /
Move Camera around an object
Hello ! I am facing a problem regarding to move camera around an object with which I did perfectly... But my camera stops its rotation after rotating the given amount And I want my camera to rotate a bit after its rotation in order to give it a good look... But I m unable to do it... Your help would be appreciated. Thanks
public Transform target;
public float distance = 5.0f;
public float xSpeed = 60.0f;
public float ySpeed = 60.0f;
public float yMinLimit = 10f;
public float yMaxLimit = 60f;
public float distanceMin = 5f;
public float distanceMax = 10f;
float x = 0.0f;
float y = 0.0f;
void Start()
{
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody) {
rigidbody.constraints = RigidbodyConstraints.FreezeRotationX;
rigidbody.constraints = RigidbodyConstraints.FreezeRotationZ;
}
}
void Update()
{
if (target && Input.touchCount == 1 && Input.GetTouch(0).position.x > Screen.width / 2 && Input.GetTouch(0).phase == TouchPhase.Moved) //Just orbit touch without movement
{
Debug.Log("Orbiting! 1 touch");
Orbit(Input.GetTouch(0));
}
else if (Input.touchCount == 2)
{
if (Input.GetTouch(0).position.x > Screen.width / 2 && Input.GetTouch(0).phase == TouchPhase.Moved)
Orbit(Input.GetTouch(0)); //Movement was touched second
else if (Input.GetTouch(1).position.x > Screen.width / 2 && Input.GetTouch(1).phase == TouchPhase.Moved)
Orbit(Input.GetTouch(1)); //Movement was touched first
}
}
void Orbit(Touch touch)
{
x += touch.deltaPosition.x * xSpeed * 0.1f /* * distance*/;
y -= touch.deltaPosition.y * ySpeed * 0.1f /* * distance*/;
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
//distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * 5, distanceMin, distanceMax);
RaycastHit hit;
if (Physics.Linecast(target.position, transform.position, out hit))
{
//distance -= hit.distance;
}
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
Answer by RedDevil · Mar 26, 2014 at 09:34 AM
You can just use the build in function called RotateAround : http://docs.unity3d.com/Documentation/ScriptReference/Transform.RotateAround.html Instead of Vector3.Zero you just put the gameobject you want to rotate around.
Answer by jogidipen · Mar 26, 2014 at 12:28 PM
First of all create empty game object at Exactly where player is situated in scene .
After that set camera a little bit far from player so that camera can view player in proper view.
**Imp:**Create Main Camera as child of empty game object IE: Empty game object will parent and main camera will chield.
after that create script
void Update () { transform.Rotate(Vector3.up, Input.GetAxis("Mouse X")); }
Apply that script on Empty game object. And run.
Thanks jogidipen ... I will try this one and will let you know soon :)
Your answer
