overiding input.acceleration

Hi guys, my player character is controlled using the accelerometer of the mobile device, however when the player reaches the end of the level and leaves the area I want the character to walk out of view automatically.

In other words, one the player character hits a collider at the edge of the level I want control to be taken away from the player and make the sprite translate along the x axis.

I’ve tried loads of ways (but want to avoid using a lerp) but nothing works.

Here is my current c# code, with this I get the error “cannot implicitly convert void to float”

using System.Collections;

public class playerMove : MonoBehaviour {
 
    public float maxSpeed; //declare the max speed

    public bool facingRight= true; // the player character is facing to the right by default

    public float move; //see move variable in fixed update

    public bool playerMovingBool = false; //is the player moving?

    public int herdCount = 1;

    Animator anim; //animator is now called anim in this script
 
    void Start ()
    {
        anim= GetComponent<Animator>(); //anim is linked to the animator
    }

    void FixedUpdate ()
    {

        move = Input.acceleration.x;

        if(move < -0.2f || move > 0.2f) // if the device is tilted so far in either direction
        {

            anim.SetFloat ("PlayerSpeed", Mathf.Abs(move)); //set the Speed parameter to the value after the comma

            rigidbody2D.velocity= new Vector2 (move * maxSpeed, rigidbody2D.velocity.y); //declare the x and y of the rigid body

            playerMovingBool = true; //the player is moving so set this bool to true. linked to the elephant game object script

            if(move > 0 && !facingRight) // if move is more than 0 and the player is NOT facing right
                Flip(); //see flip below
            else if(move < 0  && facingRight) //if move is less than 0 and facing right
                Flip(); //see flip below
            }

        else if(move > -0.1f || move < 0.2f) //stops the player sprite from gliding accross the floor
        {
            anim.SetFloat ("PlayerSpeed", 0); //set the speed float in the animator to zero in order to invoke the idle animation
            rigidbody2D.velocity= new Vector2 (0,0);
            playerMovingBool = false;
        }

    }

    public void Flip() // flips the player sprite so it is facing in the opposite direction
    {
        facingRight= !facingRight;
        Vector3 theScale= transform.localScale;
        theScale.x *= -1;
        transform.localScale= theScale;
    }

    void OnTriggerEnter2D(Collider2D col)
    {
        if (col.gameObject.tag == "exitLevel") {
            move = transform.Translate(Vector3.right * Time.deltaTime);
        }
    }
}

If you change it a little bit, to something like this:

bool done = false;
   void FixedUpdate ()
    {
       if(!done) {
             move = Input.acceleration.x;
      }

and this:

    void OnTriggerEnter2D(Collider2D col)
    {
        if (col.gameObject.tag == "exitLevel") {
            move = transform.Translate(Vector3.right * Time.deltaTime);
            done = true;
        }
    }

That ought to do the trick.

Thanks PGJ, I ended up doing something pretty much exactly the same as that :stuck_out_tongue:
Glad to know its the right way though :slight_smile:

Cheers,

Mark