Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Click to move - wall collision issue

Discussion in '2D' started by barzelas, Feb 26, 2015.

  1. barzelas

    barzelas

    Joined:
    Jan 1, 2015
    Posts:
    9
    Hi guys !
    having a tiny issue here, I'm creating a 2d mobile game and using touch input to tell my main character where to move, the issue is when I'm trying to limit the movement using invisible walls (which are basically just colliders without real walls) now the problem is this - I tap the screen to move to a certain point beyond a wall and my char moves up to the wall, upon collision I stop the movement script but a second click to that same direction will cause the char to move through the collider ignoring it completely.
    Currently I'm using Itween's simple movement scripts (also tried with lerping) and I cant find a way to solve this issue, any solutions or other ways to implement 2d limited char movement which is based on click to move?

    Thanks to anyone who gives an input !
     
  2. Naeja

    Naeja

    Joined:
    Jul 10, 2014
    Posts:
    17
    Can you post some of your relevant code? When you say stop the movement script do you mean pause it? Deactive it? Or just set velocity to zero? Keep in mind the collider won't trigger twice. ie if you script says "stop moving OnTrigger" and you're already in the trigger it won't call the OnTrigger() method. It could also be an issue of collider size/speed
     
  3. barzelas

    barzelas

    Joined:
    Jan 1, 2015
    Posts:
    9
    That's exactly the issue, I'm using Itween's pause on collision, and once there is a second click the collision does not occur again, that's what I'm trying to solve, or if possible find a simple alternative solution to character movement in a 2d game :)

    The relevant code is something like this -
    upload_2015-2-26_17-41-59.png
     
  4. Naeja

    Naeja

    Joined:
    Jul 10, 2014
    Posts:
    17
    Really don't recommend using iTween for Player movement. iTween is an animations system it shouldn't be used as a replacement for the physics system. I'm not sure what your collider.gameObject == invisWall is doing as invisWall isn't defined in the code you posted.
     
  5. barzelas

    barzelas

    Joined:
    Jan 1, 2015
    Posts:
    9
    Its just an invisible object surrounding a room, so when you try to break out of the room you hit the collider, thus if you hit that invisible wall your movement should be stopped because you're not allowed to move through walls, but regardless of the way I wrote it, what would you recommend for 2d player movement restricting ? (I'm kinda new to UNITY and I'm just trying random things I find on the net), I ruled out using forces and given velocity as its not something that works well with char movement (as far as I can tell) because of the fact that char movement is not physics related movement.

    Thanks for the fast responses by the way, really appreciated !
     
  6. Naeja

    Naeja

    Joined:
    Jul 10, 2014
    Posts:
    17
    You can do something as simple as
    transform.position += transform.right * (velocity * Time.deltaTime * Input.GetAxis("Horizontal"));