Accelerometer moving limit. Help me please!

Please help me! I had a problem with my final project game 2D, i tried to use accelerometer control for my project, but i had a problem, my character game (motor) still can move out of the track if i tilt my android device. if i tilt to the left, character wil get stuck on the left of track, if i tilt to the right, character will be out of the track. i want make a limit position for the character but i dont know the code to make limit access for my character, i tried many different way on youtube to make that happen, but still didnt work. if somebody can help me, im very thankful for that :slight_smile: please…

this is my script character controller:

using UnityEngine;
using System.Collections;

public class motorController : MonoBehaviour {

   
    public float motorSpeed;
    public float maxPos = 2.5f;
    

    Vector3 position;
    public uiManager ui;
    public audioManager am;
    public audioManager ad;
    

    private scoreManager theScoreManager;

    void Awake() {
        am.motorSound.Play();
    }


	// Use this for initialization
	void Start () {

        position = transform.position;

        theScoreManager = FindObjectOfType<scoreManager>();

	}
	
	// Update is called once per frame
	void Update () {

        theScoreManager.scoreIncreasing = true;

        // Coding accerleration move character
        float temp = Input.acceleration.x ;

        if ((transform.position.x < 2.5 && Input.acceleration.y > 0) || (transform.position.x > -2.5 && Input.acceleration.y < 0))

            transform.Translate(temp, 0, 0 * motorSpeed);

    }

    void OnCollisionEnter2D(Collision2D col){
        if(col.gameObject.tag == "mobil musuh")  {
            Destroy (gameObject);
            ui.gameOverActivated();
            am.motorSound.Stop();
            ad.crashSound.Play();


            theScoreManager.scoreCount = 0;
            theScoreManager.scoreIncreasing = false;

        }
    }


}

THIS THE PICT MY GAME 2D:

Before moving your object you should check where it is going to move when applying force.
so the right way of doing this is:

  • get motor position
  • get input.acceleration
  • add those together and check if the bike is going to be in the track
  • if it is, move the bike to the position