- Home /
Question by
siddharth3322 · Jan 15, 2016 at 08:31 AM ·
cameraunity5rotatecamera rotaterotatearound
Based on touch rotate camera object
Based on player touch I want to rotate my camera around my main gameobject. You need to consider following game play so that you got better idea about what I want.
I want to rotate my camera around this object. I have written one script for this but can't able to get smooth result from this.
public class RollCamera : MonoBehaviour
{
Vector3 prevMousePosition;
float turnSpeed = 2f;
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
prevMousePosition = Camera.main.ScreenToViewportPoint (Input.mousePosition);
} else if (Input.GetMouseButton (0)) {
Vector3 currMousePosition = Camera.main.ScreenToViewportPoint (Input.mousePosition);
Vector3 diffMousePos = prevMousePosition - currMousePosition;
float temp = diffMousePos.x;
diffMousePos.x = diffMousePos.y;
diffMousePos.y = -temp;
transform.RotateAround(transform.position, diffMousePos, 4f);
prevMousePosition = currMousePosition;
}
}
}
My Inspector structure is like this :
My camera is 10 units away from its centre. I want to rotate my camera around this Camera Centre point. Please give me some help in this. If you need any kind of more information then ask me any time for this.
EDIT :
I have written one more script for this but can't able to get smooth result form this also.
public class CameraPanning : MonoBehaviour
{
// Update is called once per frame
void LateUpdate ()
{
// If there are two touches on the device...
if (Input.touchCount == 1) {
// Store currnet touch.
Touch touch = Input.GetTouch (0);
Vector3 axis = touch.deltaPosition;
transform.RotateAround (transform.position, axis, 4f);
}
}
}
Rotation happen like camera moves in random direction.
screen-shot-2016-01-15-at-15946-pm.png
(58.3 kB)
screen-shot-2016-01-15-at-15721-pm.png
(268.3 kB)
Comment