x


Smooth Camera help

I am trying to get a camera to follow my 2D character smoothly and am having issues. It seems that the camera appears to work for a while and then when it goes too far to the left or right it just keeps moving until I am off screen. Here is my code if anyone wants to help me. I dont know what I am doing wrong. to me my code says if the camera is 50 units to the left or right of the character move towards the character, but perhaps i am missing something. PLEASE someone help me. ps: mrLock is my character i am calling upon.

var mrLock;
var cameraSpeed;
var distanceFromCharacterX;

function Awake()
{
    cameraSpeed = .25;
    distanceFromCharacterX = 50;
    mrLock = (GameObject.FindGameObjectWithTag("DeadLock")).GetComponent(deadLock);
}

function Update ()
{
    if(mrLock.transform.position.x - transform.position.x >= distanceFromCharacterX)
       {
         transform.position.x +=  mrLock.transform.position.x * Time.deltaTime * cameraSpeed;
       }
    else if(mrLock.transform.position.x - transform.position.x <= -distanceFromCharacterX)
       {
         transform.position.x +=  mrLock.transform.position.x * Time.deltaTime * cameraSpeed;
       }
}
more ▼

asked Oct 13 '11 at 09:02 PM

Saccoon gravatar image

Saccoon
1 1 1

(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3003
x316
x207

asked: Oct 13 '11 at 09:02 PM

Seen: 488 times

Last Updated: Oct 13 '11 at 09:02 PM