1

I am trying to make the following camera panning js script, to work as it should, meaning panning the camera left and right. What I have accomplished till now, is to move camera only left and back to its starting position. I can't get it move left and right on a gui.button being clicked/touched.

Here is the js script:

    #pragma strict

var target : GameObject;
var xpositionLeft = 10;
var xpositionRight = -10;
//var smooth : float = 5; // Don't know where should I put this var to make it pan smooth?

private static var isPanning = false;

function Update()
{
if(isPanning == true)
{
transform.position.x = target.transform.position.x;
transform.position.x = xpositionLeft;
    }
    else
    {
    transform.position.x = target.transform.position.x; // It only pan the camera left, not right!
    transform.position.x = xpositionRight;
    }
}

static function doPanning ()
    {
        isPanning = !isPanning;
    }

Can someone give a clue on how to make this script work? I'm new to Unity and programming, so any help is more than welcomed. Thank you all in advance for your time, and your answers.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
CodeBugging
  • 321
  • 12
  • 28
  • It's unclear what exactly you're trying to achieve. Should the camera pan left and right relative to the `target` object? Are there separate buttons for left and right? – Stefan Hoffmann Apr 30 '14 at 22:31
  • Hello @Leosori, thanks for answering my question. Well, this script it is attached to MainCamera, and it is function on pressing two GUI.Buttons, one for panning left, the other for panning right. As it is now, the panning left is working, not the panning right. Can you advice me where is the problem in this coding? – CodeBugging Apr 30 '14 at 23:00
  • what about the `target`? Should the camera only switch sides relative to the `target` or do you want to move the camera freely and it should move further to one side each time the button is pressed? – Stefan Hoffmann May 01 '14 at 00:45
  • Well @Leosori, at the moment the code I've posted, moves the camera relative to the target side by side, but in a standard integer. If you could post a snippet to move the camera freely or move further to each side each time the gui.button is pressed, it would be more than wellcomed. You see I'm not that experienced to have this functionality to my coding. Thank you again for your time. – CodeBugging May 01 '14 at 00:59

1 Answers1

1

There are several problems with your code. First, the lines transform.position.x = target.transform.position.x; don't have any effect, because you're immediately overwriting it in the next line. Your code basically only flips transform.position.x between -10 and 10.

Second, the behavior you expect doesn't match the logic in your code. You have only two states, isPanning true and false, but you need three states: pan left, pan right and do nothing.

// how far the camera should move witch each click
var xPositionOffset = 10;

// -1 = left, 0 = do dothing, 1 = right
var panDirection = 0;

function Update()
{
    if (panDirection != 0) // pan left/right
    {
        transform.position.x = transform.position.x + (panDirection * xPositionOffset);
        panDirection = 0;
    }
}

Now you only need to set the variable panDirection to -1 or 1 if one your buttons is pressed and the camera will move farther to that side.

If you're having trouble with the vector arithmetic, have a look at the chapter 'Understanding Vector Arithmetic' from the Unity manual.

The above code will move the camera not very smooth, but it's easier to understand the basic concept this way. You can use the function Vector3.MoveTowards to get a smoother movement and the example showed in the linked reference should help you to implement it.

Stefan Hoffmann
  • 3,214
  • 16
  • 30
  • Hello @Leosori. Your answer and your recommendations on how to implement the camera panning functionality to my app, put me on the right track. Therefore, I am marking your answer as the correct one, and your recommendation as useful. Thank you again. – CodeBugging May 01 '14 at 10:42