Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Extra Rotation on Camera

Discussion in 'Editor & General Support' started by pagan, Jun 6, 2011.

  1. pagan

    pagan

    Joined:
    Nov 8, 2010
    Posts:
    28
    Right I am trying to do this since few days and I haven't succed yet:
    basically I have a camera which rotates around an object when using the keys left,right,up and down(also zooms).
    I am using this script:

    Code (csharp):
    1. var target : Transform;
    2. var edgeBorder = 0.1;
    3. var horizontalSpeed = 360.0;
    4. var verticalSpeed = 120.0;
    5. var minVertical = 20.0;
    6. var maxVertical = 85.0;
    7. var minDistance = 1; //Min distance of the camera from the target
    8. var maxDistance = 20;
    9.  
    10. private var x = 0.0;
    11. private var y = 0.0;
    12. private var distance = 0.0;
    13.  
    14. function Start(){
    15.     x = transform.eulerAngles.y;
    16.     y = transform.eulerAngles.x;
    17.     distance = (transform.position - target.position).magnitude;
    18. }
    19.  
    20. function LateUpdate(){
    21.  
    22.    //Zooming with mouse
    23.    distance += Input.GetAxis("Mouse ScrollWheel")*distance;
    24.    distance = Mathf.Clamp(distance, minDistance, maxDistance);
    25.  
    26.     var dt = Time.deltaTime;
    27.     x -= Input.GetAxis("Horizontal") * horizontalSpeed * dt;
    28.     y += Input.GetAxis("Vertical") * verticalSpeed * dt;
    29.    
    30.     y = ClampAngle(y, minVertical, maxVertical);
    31.      
    32.     var rotation = Quaternion.Euler(y, x, 0);
    33.     var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
    34.    
    35.     transform.rotation = rotation;
    36.     transform.position = position;
    37.    
    38. }
    39.  
    40. static function ClampAngle (angle : float, min : float, max : float) {
    41.     if (angle < -360)
    42.         angle += 360;
    43.     if (angle > 360)
    44.         angle -= 360;
    45.     return Mathf.Clamp (angle, min, max);
    46. }
    47.  
    48.  
    49. function buttonRightRotation(){
    50. transform.Rotate(Vector3.up * 45);
    51. print("Doooone");
    52.  
    53. }
    I have added the function buttonRightRotation() which I call from another script attached to a gui botton:

    Code (csharp):
    1. var rotateCameraZoom : RotateCameraZoom;
    2. var target : Transform;
    3. function OnMouseDown () {
    4.  rotateCameraZoom.buttonRightRotation();
    5. }
    This is in order to have a extra interface control on rotation(eventually I will have for bottons:left, right, up and down)

    At the moment this does not produce anything, it gives me no errors.
    Does anyone knows the solution for this?
     
  2. pagan

    pagan

    Joined:
    Nov 8, 2010
    Posts:
    28
    OK I found out that it only works if I switch off the first script:confused:
    In brief how do I modify the scripts so that I perform the same action of the arrow keyboard keys also when I press the gui button?
     
    Last edited: Jun 6, 2011