This is from the scripting reference of Mathf.Clamp
:
// Set the position of the transform to be that of the time
// but never less than 1 or more than 3
function Update ()
{
transform.position.x = Mathf.Clamp(Time.time, 1.0, 3.0);
}
I dont know if your error is the result of c# syntax (2.f), or if you can do that in js also. Hope it helps
Weird I tested both ways and the movement looked identical to me… maybe Translate is doing something different under the hood
you could try changing the script to :
var speed = 10.0;
function Update () {
var translation = Input.GetAxis ("Horizontal") * speed;
translation *= Time.deltaTime;
transform.Translate (translation, 0, 0);
if (transform.position.x < -2.0)
transform.position.x = -2.0;
if (transform.position.x > 2.0)
transform.position.x = 2.0;
}
it uses Translate as before and just checks if it’s gone past the extremes and sets it back. Kind of clunky, but it might sidestep the jumpiness issue.