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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Restrain 2d Sprite's movement so it doesn't go off-screen.

Discussion in '2D' started by DennisDuty, Jun 29, 2015.

  1. DennisDuty

    DennisDuty

    Joined:
    Jun 24, 2015
    Posts:
    3
    Hey everybody... I'm really struggling here and you all seem so good with this stuff.

    I'm allowing a sprite to move back and forth along the x axis. There's no jumping or gravity or physics or anything so I set it to "is kinematic".

    Now I have no way of keeping the player on screen. I can't seem to figure out a non-physics way to stop the player from just walking off the screen. This is my player control script so far:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Simplecontrols : MonoBehaviour {
    5.     public float speed;
    6.     Animator anim;
    7.  
    8.     void Start()
    9.     {
    10.         anim = GetComponent<Animator> ();
    11.     }
    12.  
    13.  
    14.     void Update ()
    15.     {
    16.         xmovement();
    17.     }
    18.  
    19.  
    20.     void xmovement()
    21.     {
    22.     if (Input.GetAxisRaw ("Horizontal") > 0)
    23.         {
    24.             anim.SetBool ("WalkRight", true);
    25.             transform.Translate (Vector2.right * speed * Time.deltaTime);
    26.         } else
    27.         {
    28.             anim.SetBool ("WalkRight", false);
    29.         }
    30.     if (Input.GetAxisRaw ("Horizontal") < 0)
    31.         {
    32.             anim.SetBool ("WalkLeft", true);
    33.             transform.Translate (Vector2.left * speed * Time.deltaTime);
    34.         } else
    35.         {
    36.             anim.SetBool ("WalkLeft", false);
    37.         }
    38.     }
    39. }
    Any advice or suggestions to achieve this would be appreciated.

    The game is a root-beer tapper style tower defense. Reference for those who don't know rootbeertapper:


    Guidance on on how to go about 'jumping' from one walkable row to the next would help a lot... since that's the next thing I'll be working on.
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Here a version of your script which adds check for the edge of the screen and "jumping". It's pretty quick and dirty, but it should get you started.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Runtime.Remoting.Messaging;
    4.  
    5. public class Simplecontrols : MonoBehaviour {
    6.     public float speed;
    7.     Animator anim;
    8.  
    9.     float[] yPositions =
    10.     {
    11.         7f,
    12.         3.5f,
    13.         0f,
    14.         -3.5f,
    15.         -7f
    16.     };
    17.  
    18.     int yPosition = 0;
    19.  
    20.     float width;
    21.  
    22.     void Start()
    23.     {
    24.         anim = GetComponent<Animator>();
    25.  
    26.         width = GetComponent<Renderer>().bounds.extents.x;
    27.     }
    28.  
    29.     void Update()
    30.     {
    31.         xmovement();
    32.     }
    33.  
    34.  
    35.     void xmovement()
    36.     {
    37.         float minScreen = Camera.main.ViewportToWorldPoint (Vector2.zero).x;
    38.         float maxScreen = Camera.main.ViewportToWorldPoint(Vector2.one).x;
    39.  
    40.         Vector2 pos = transform.position;
    41.  
    42.         if (Input.GetAxisRaw("Horizontal") > 0)
    43.         {
    44.             anim.SetBool("WalkRight", true);
    45.             pos.x += speed * Time.deltaTime;
    46.         }
    47.         else
    48.         {
    49.             anim.SetBool("WalkRight", false);
    50.         }
    51.  
    52.         if (pos.x + width > maxScreen)
    53.         {
    54.             pos.x = maxScreen - width;
    55.         }
    56.  
    57.         if (Input.GetAxisRaw("Horizontal") < 0)
    58.         {
    59.             anim.SetBool("WalkLeft", true);
    60.             pos.x -= speed * Time.deltaTime;
    61.         }
    62.         else
    63.         {
    64.             anim.SetBool("WalkLeft", false);
    65.         }
    66.  
    67.         if (pos.x - width < minScreen)
    68.         {
    69.             pos.x = minScreen + width;
    70.         }
    71.  
    72.         if (Input.GetKeyDown(KeyCode.UpArrow))
    73.         {
    74.             yPosition--;
    75.  
    76.             if (yPosition < 0)
    77.             {
    78.                 yPosition = 0;
    79.             }
    80.         }
    81.  
    82.         if (Input.GetKeyDown(KeyCode.DownArrow))
    83.         {
    84.             yPosition++;
    85.  
    86.             if (yPosition >= yPositions.Length)
    87.             {
    88.                 yPosition = yPositions.Length-1;
    89.             }
    90.         }
    91.  
    92.         pos.y = yPositions[yPosition];
    93.  
    94.         transform.position = pos;
    95.     }
    96. }
    97.  
     
  3. DennisDuty

    DennisDuty

    Joined:
    Jun 24, 2015
    Posts:
    3
    I had no idea how to progress. This gives me a direction to work towards.
    You've been so helpful, thanks!
     
  4. DennisDuty

    DennisDuty

    Joined:
    Jun 24, 2015
    Posts:
    3
    I've been playing around and I still can't seem to restrict X movement.
    I changed the if statement to:

    Code (CSharp):
    1.  
    2. if (pos.x < 1)
    3.         {
    4.             pos.x = 1;
    5.         }
    6.  
    Trying to get ANY affect on gameplay... but the guy just keep walking right off screen if he wants, no bounds or limits.
     
  5. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    I've just tried the script again and it works perfectly for me. Are you using an orthographic camera? Then again, since your change didn't work either that shouldn't be the problem. Are there any other scripts on your guy that might interfer?