Quantcast
Channel: Answers for "Player movement boundaries"
Viewing all articles
Browse latest Browse all 4

Answer by Jaap Kreijkamp

$
0
0
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.


Viewing all articles
Browse latest Browse all 4

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>