Question by
studioglitch · Jul 29, 2017 at 12:06 PM ·
c#scripting problemmovementaccelerometerclamp
How do i limit my accelerometer movement
So i have a topdown space shooter type of game and i need to limit its horizontal movement ` public class rocketMove : MonoBehaviour {
public class rocketMove : MonoBehaviour {
public float rocketSpeed;
// Use this for initialization
void Start () {
Screen.sleepTimeout = SleepTimeout.NeverSleep;
}
// Update is called once per frame
void Update()
{
float temp = Input.acceleration.x;
if (GetComponent<PUCol>().isInverted == false)
{
transform.Translate(temp * Time.deltaTime * rocketSpeed, 0, 0);
}
if (GetComponent<PUCol>().isInverted == true)
{
transform.Translate(-temp * Time.deltaTime * rocketSpeed, 0, 0);
}
this is the code i have for movement, i need to limit the movement in between -2.8f and 2.8f. Please help im lost :/
Comment
Answer by jandd661 · Jul 29, 2017 at 02:38 PM
Try Mathf.Clamp
https://docs.unity3d.com/ScriptReference/Mathf.Clamp.html
transform.position.x = Mathf.Clamp(transform.position.x, -2.8f, 2.8f)