Tiny Camera Pan (Should Be Easy)

Discussion in 'Scripting' started by youngapprentice, May 1, 2012.

  1. youngapprentice

    youngapprentice

    Member

    Joined:
    Apr 10, 2010
    Messages:
    254
    Hello again, my party people! :cool:
    I have a reticle that is layered and centered over the mouse cursor just like any other FPS, but in my game the camera doesn't follow the mouse cursor. Yes I want it to be that way.

    What I'd like to do is the have the camera slightly pan up, down, left, or right a certain number of degrees before ceasing to pan when the cursor touches the top, bottom, left, or right borders of the screen.

    How would I achieve this? I'd need to check the mouse position for the -x, x, y, and -y, coordinates and then what? How would I slowly pan the camera in the given direction???

    Thank you in advance! :)

    An image for assistance:[​IMG]

    EDIT/UPDATE:

    I have written the code. Here it is:
    Code (csharp):
    1. var leftSide : float;
    2. var rightSide : float;
    3. var Top : float;
    4. var Bottom : float;
    5. function Update () {
    6. //Does the mouse hit the Left area?
    7.     if(Input.mousePosition.x <= leftSide){
    8.         Debug.Log("Hit Left");
    9.         transform.Translate(-0.02,0,0);
    10.         transform.Rotate(Vector3.down*0.1);
    11.         }
    12. //Does the mouse hit the Right area?
    13.     if(Input.mousePosition.x >= rightSide){
    14.         Debug.Log("Hit Right");
    15.         transform.Translate(0.02,0,0);
    16.         transform.Rotate(Vector3.up*0.1);
    17.         }
    18. //Does the mouse hit the Top area?
    19.     if(Input.mousePosition.y >= Top){
    20.         Debug.Log("Hit Top");
    21.         transform.Translate(0,0.02,0);
    22.         transform.Rotate(Vector3.left*0.1);
    23.         }
    24. //Does the mouse hit the Bottom area?
    25.     if(Input.mousePosition.y <= Bottom){
    26.         Debug.Log("Hit Bottom");
    27.         transform.Translate(0,-0.02,0);
    28.         transform.Rotate(Vector3.right*0.1);
    29.         }
    30. }
    This code identifies all four zones and moves the camera accordingly. Which is fine and dandy. Here is what I really need help with. I have three things I need to do:

    1. Have the camera latch onto the mouse when it enters any of the four zones

    2. Set a limit on how far the camera can drift

    3. Have the camera go back to the original static position (Unparenting it from the mouse) when the mouse moves back to the center zone


    What was this about Lerps??? Thanks!- YA
    Last edited: May 2, 2012
  2. DexRobinson

    DexRobinson

    Member

    Joined:
    Jul 26, 2011
    Messages:
    581
    What I am thinking you could do is like this,

    Code (csharp):
    1.  
    2. void Update()
    3.     {
    4.         if (Input.mousePosition.x < Screen.width / 4)
    5.         {
    6.             transform.eulerAngles -= new Vector3(0f, 0.05f, 0f);
    7.         }
    8.         else if (Input.mousePosition.x > Screen.width - (Screen.width / 4))
    9.         {
    10.             transform.eulerAngles += new Vector3(0f, 0.05f, 0f);
    11.         }
    12.         else if (Input.mousePosition.y > Screen.height - (Screen.height / 5))
    13.         {
    14.             transform.eulerAngles -= new Vector3(0.05f, 0f, 0f);
    15.         }
    16.         else if (Input.mousePosition.y < Screen.height / 5)
    17.         {
    18.             transform.eulerAngles += new Vector3(0.05f, 0f, 0f);
    19.         }
    20.         else
    21.         {
    22.             transform.eulerAngles = new Vector3(0f, 0.0f, 0f);
    23.         }
    24.     }
    25.  
    Or use transform.position to move the camera, euler is for rotating the camera. And then you could use a lerp for a smooth transition back to the original position.
  3. Zethariel1

    Zethariel1

    New Member

    Joined:
    Mar 21, 2012
    Messages:
    439
    I would Lerp the camera rotation vector (in both axes), third parameter would be mousePosition/Screen.height/widht.
  4. youngapprentice

    youngapprentice

    Member

    Joined:
    Apr 10, 2010
    Messages:
    254
    I don't quite understand what you guys are saying. I will update the first post as well. This is what I have so far:

    Code (csharp):
    1. var leftSide : float;
    2. var rightSide : float;
    3. var Top : float;
    4. var Bottom : float;
    5. function Update () {
    6. //Does the mouse hit the Left area?
    7.     if(Input.mousePosition.x <= leftSide){
    8.         Debug.Log("Hit Left");
    9.         transform.Translate(-0.02,0,0);
    10.         transform.Rotate(Vector3.down*0.1);
    11.         }
    12. //Does the mouse hit the Right area?
    13.     if(Input.mousePosition.x >= rightSide){
    14.         Debug.Log("Hit Right");
    15.         transform.Translate(0.02,0,0);
    16.         transform.Rotate(Vector3.up*0.1);
    17.         }
    18. //Does the mouse hit the Top area?
    19.     if(Input.mousePosition.y >= Top){
    20.         Debug.Log("Hit Top");
    21.         transform.Translate(0,0.02,0);
    22.         transform.Rotate(Vector3.left*0.1);
    23.         }
    24. //Does the mouse hit the Bottom area?
    25.     if(Input.mousePosition.y <= Bottom){
    26.         Debug.Log("Hit Bottom");
    27.         transform.Translate(0,-0.02,0);
    28.         transform.Rotate(Vector3.right*0.1);
    29.         }
    30. }
    This code identifies all four zones and moves the camera accordingly. Which is fine and dandy. Here is what I really need help with. I have three things I need to do:

    1. Have the camera latch onto the mouse when it enters any of the four zones

    2. Set a limit on how far the camera can drift

    3. Have the camera go back to the original static position (Unparenting it from the mouse) when the mouse moves back to the center zone


    What was this about Lerps??? Thanks!- YA
  5. Zethariel1

    Zethariel1

    New Member

    Joined:
    Mar 21, 2012
    Messages:
    439
    While I was writing, I actually came up with a different solution :) Untested.

    Code (csharp):
    1.  
    2. float CameraPan; // set how much the camera can pan;
    3. float xAxis = Input.mousePosition.x - Screen.width/2;
    4.  
    5. if ( Mathf.Abs(xAxis) > Screen.width*4/5 ) // if the mouse position is either in the left or right side of the screen (20% of the screen is taken into consideration)
    6. {
    7.      camera.position.localEulerAngles = Vector3.ClampMagnitude(new Vector3 ( 0, Input.GetAxis( "Mouse X" ) , 0 ), CameraPan); // change camera rotation up to a certain degree
    8. } else
    9.       camera.position.localEulerAngles = Vector3.zero; // make sure camera returns to center if we are no longer panning
    10.  
    You can do the same with up and down.

    Sorry it's in C#, but conversion shouldn't be much of a problem :)
    Last edited: May 2, 2012
  6. youngapprentice

    youngapprentice

    Member

    Joined:
    Apr 10, 2010
    Messages:
    254
    Oh, BABY! Thank you Zethariel! This is exactly what I need. I will implement this into the code later today or when I have the time and will update with results. Thanks guys! :)-YA
  7. youngapprentice

    youngapprentice

    Member

    Joined:
    Apr 10, 2010
    Messages:
    254
  8. Zethariel1

    Zethariel1

    New Member

    Joined:
    Mar 21, 2012
    Messages:
    439
  9. youngapprentice

    youngapprentice

    Member

    Joined:
    Apr 10, 2010
    Messages:
    254
    Oh! So you are saying that the script responds to any movement on the x axis from the mouse. Genius. I failed the first time trying to convert it to javascript, but I'll try again. Thanks! - YA
  10. youngapprentice

    youngapprentice

    Member

    Joined:
    Apr 10, 2010
    Messages:
    254
    I was able to successfully implement it BUT I need to know this: Is there any way to change the activation zone to the boundaries of the GAME SCREEN instead of the ENTIRE SCREEN? No I am not yelling at you. Just wanted to make the words stand out :D
    Thanks!-YA