x


Player movement boundaries

Here is my movement code for the player:

function Go ()
{
horizontal = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
vertical = Input.GetAxis("Vertical") * speed * Time.deltaTime;

if (transform.position.x > 7 || transform.position.x < -7)
{
    if (transform.position.x > 7)
        transform.position.x = 7;
    else
        transform.position.x = -7;

    horizontal = 0;
}
else
    transform.Translate(horizontal,0,0);

if (transform.position.y > 9 || transform.position.y < -1)
    if (transform.position.y > 9)
        transform.position.y = 9;
    else
        transform.position.y = -1;
else
    transform.Translate(0,vertical,0);
}

(WARNING: PSEUDO CODE AHEAD) My problem is when the player attempts to move outside of the boundaries, they do for a split second, THEN the code forces them back. This turns into a player constantly twitching if they constantly try to cross the boundary. What I want, is to grab the player's position from what would happen if they attempted to cross. This is what I mean by what I want:

if(transform.Translate(horizontal,0,0) makes transform.position.x == 7 or -7)
transform.position.x = 7 or -7;
//depending on which side of the screen they're on.

Now I guess that I could make this happen by having a clone box off screen move first, as in:

if(clone.transform.Translate(horizontal,0,0) makes
clone.transform.position.x == 7 or -7)
transform.position.x = 7 or -7;

But I think that would eat up too much resources, and that there is a better solution. Or am I wrong?

more ▼

asked Mar 15 '10 at 12:24 AM

The BOOM gravatar image

The BOOM
283 19 26 39

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

1 answer: sort voted first
function Go() {
    horizontal = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
    vertical = Input.GetAxis("Vertical") * speed * Time.deltaTime;

    var pos : Vector3 = transform.position;
    pos.x = Mathf.Clamp(pos.x + horizontal, -7, 7);
    pos.y = Mathf.Clamp(pos.y + vertical, -7, 7);
    transform.position = pos;
}

The link Lipis gave in comment indeed explains the Clamp function. Basically it keeps the value within a certain range so with Mathf.Clamp(value, -7, 7), if value is less than -7, function returns -7, bigger than 7, function returns 7, otherwise it just returns value.

So by making sure we never set the value outside the allowed range we won't get jittering.

more ▼

answered Mar 15 '10 at 12:44 AM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
6.4k 20 26 70

Thanks, but could I also have an explanation?

Mar 15 '10 at 12:55 AM The BOOM
Mar 15 '10 at 01:18 AM Lipis

I can't vote this up, (yet), but I just wanted to add that the last 4 lines of the script above work amazingly well.

Dec 29 '12 at 12:51 AM samsmithnz
(comments are locked)
10|3000 characters needed characters left
Your answer
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:

x1374
x957

asked: Mar 15 '10 at 12:24 AM

Seen: 3382 times

Last Updated: Dec 29 '12 at 12:51 AM