Unity Community


Results 1 to 10 of 10

  1. Location
    Michigan
    Posts
    253

    Tiny Camera Pan (Should Be Easy)

    Hello again, my party people!
    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:

    EDIT/UPDATE:

    I have written the code. Here it is:
    Code:  
    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 by youngapprentice; 05-01-2012 at 07:14 PM.
    Current games:

    Asteroid Field (Unity)

    Dress up Obama (Flash)

    Try 'em out! More great games by YA Productions are on their way!


  2. Posts
    430
    What I am thinking you could do is like this,

    Code:  
    1. void Update()
    2.     {
    3.         if (Input.mousePosition.x < Screen.width / 4)
    4.         {
    5.             transform.eulerAngles -= new Vector3(0f, 0.05f, 0f);
    6.         }
    7.         else if (Input.mousePosition.x > Screen.width - (Screen.width / 4))
    8.         {
    9.             transform.eulerAngles += new Vector3(0f, 0.05f, 0f);
    10.         }
    11.         else if (Input.mousePosition.y > Screen.height - (Screen.height / 5))
    12.         {
    13.             transform.eulerAngles -= new Vector3(0.05f, 0f, 0f);
    14.         }
    15.         else if (Input.mousePosition.y < Screen.height / 5)
    16.         {
    17.             transform.eulerAngles += new Vector3(0.05f, 0f, 0f);
    18.         }
    19.         else
    20.         {
    21.             transform.eulerAngles = new Vector3(0f, 0.0f, 0f);
    22.         }
    23.     }

    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.
    Taptoon Games Facebook
    Auto-Magic Save System - the fastest way to save your games.


  3. Posts
    439
    I would Lerp the camera rotation vector (in both axes), third parameter would be mousePosition/Screen.height/widht.
    Looking for helpfull hands with an action/RPG fan game -- link
    Remember to have a nice day and smile!


  4. Location
    Michigan
    Posts
    253
    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:  
    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
    Current games:

    Asteroid Field (Unity)

    Dress up Obama (Flash)

    Try 'em out! More great games by YA Productions are on their way!


  5. Posts
    439
    While I was writing, I actually came up with a different solution Untested.

    Code:  
    1. float CameraPan; // set how much the camera can pan;
    2. float xAxis = Input.mousePosition.x - Screen.width/2;
    3.  
    4. 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)
    5. {
    6.      camera.position.localEulerAngles = Vector3.ClampMagnitude(new Vector3 ( 0, Input.GetAxis( "Mouse X" ) , 0 ), CameraPan); // change camera rotation up to a certain degree
    7. } else
    8.       camera.position.localEulerAngles = Vector3.zero; // make sure camera returns to center if we are no longer panning

    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 by Zethariel1; 05-02-2012 at 12:21 AM.
    Looking for helpfull hands with an action/RPG fan game -- link
    Remember to have a nice day and smile!


  6. Location
    Michigan
    Posts
    253
    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
    Current games:

    Asteroid Field (Unity)

    Dress up Obama (Flash)

    Try 'em out! More great games by YA Productions are on their way!


  7. Location
    Michigan
    Posts
    253
    In the line where it says

    camera.position.localEulerAngles = Vector3.ClampMagnitude(new Vector3 ( 0, Input.GetAxis( "Mouse X" ) , 0 ), CameraPan); // change camera rotation up to a certain degree


    What is the 'Input.GetAxis("Mouse X)"???
    What do I put there? Thanks!-YA
    Current games:

    Asteroid Field (Unity)

    Dress up Obama (Flash)

    Try 'em out! More great games by YA Productions are on their way!


  8. Posts
    439
    Looking for helpfull hands with an action/RPG fan game -- link
    Remember to have a nice day and smile!


  9. Location
    Michigan
    Posts
    253
    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
    Current games:

    Asteroid Field (Unity)

    Dress up Obama (Flash)

    Try 'em out! More great games by YA Productions are on their way!


  10. Location
    Michigan
    Posts
    253
    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
    Thanks!-YA
    Current games:

    Asteroid Field (Unity)

    Dress up Obama (Flash)

    Try 'em out! More great games by YA Productions are on their way!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •