Jumping Between Walls Help - Can't get it working?

Hey everybody,
I’m working on a game that is similar to Ninjump, where a single tap will make the player jump from one side of the screen to the other. While I’m waiting for art, I’m just using a Cube for the player and walls. I’m not really sure what the best way to make this happen is, and was hoping that you guys could help.
I have my left wall at -4, right wall at 4, and the player at -3. During the game, a hit of the spacebar will move the player directly across the game play area on the same Y value, but on the opposite wall.
Here is my current jump method, which crashes on start up (it is called by a Director object, which takes care of all user input. It calls this method upon hit of the spacebar):

public class onJump : MonoBehaviour {
    bool isRight = false; //true if player is on right wall, false otherwise
    bool canJump = true;

    public void Bounce(){
        if (isRight && canJump) {       
            Debug.Log ("Move left!");
            transform.Translate(new Vector3(this.transform.position.x - 6, 0, 0) * Time.deltaTime);
        } else if(!isRight && canJump) {
            Debug.Log ("Move right!");
            transform.Translate(new Vector3(this.transform.position.x + 6, 0, 0) * Time.deltaTime);
        }
        //isRight = !isRight;
        StartCoroutine (timer());
    }

    IEnumerator timer(){
        canJump = false;
        yield return new WaitForSeconds (2);
        canJump = true;
    }
}

Thank you for the help :slight_smile:

Try something like this:

using System;
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class onJump : MonoBehaviour
{
    bool isRight = false; //true if player is on right wall, false otherwise
    bool canJump = true;

    private float movement = 0;

    void Update()
    {
        if (Input.anyKeyDown)
        {
            Bounce();
        }
    }

    void FixedUpdate()
    {
        if (movement != 0)
        {
            Vector2 pos = transform.position;

            pos.x += movement * Time.deltaTime;

            if (Mathf.Abs(pos.x) > 4)
            {
                pos.x = 4 * Math.Sign(pos.x);

                movement = 0;
            }

            rigidbody2D.MovePosition(pos);
        }
    }

    public void Bounce()
    {
        if (isRight && canJump)
        {
            Debug.Log("Move left!");
            movement = -10;
        }
        else if (!isRight && canJump)
        {
            Debug.Log("Move right!");
            movement = 10;
        }

        isRight = !isRight;

        StartCoroutine(timer());
    }

    IEnumerator timer()
    {
        canJump = false;
        yield return new WaitForSeconds(2);
        canJump = true;
    }
}

Thank you! That worked!