Search Unity

How to not allow hero to "move out of camera"?

Discussion in '2D' started by sotrosh, Nov 20, 2018.

  1. sotrosh

    sotrosh

    Joined:
    Dec 25, 2014
    Posts:
    13
    Hi,

    I'm working on 2d runner game where ship(two circle collision 2d and rigidbody 2d) goes across waves (edge collision 2d objects). User uses left/right arrows to move ship forward/back:

    ship_runner.png

    The main problem I'm stuck is imao quite simple: how to prevent ship go out of the camera view when user press left/right? :(

    This code doesn't work so accurate: mostly ship goes out camera view, but sometimes not (especially if move VERY slow):

    Code (CSharp):
    1.     void FixedUpdate()
    2.     {
    3.         if (this.isLeftArrowButtonPressing)
    4.         {
    5.             Vector2 direction = (Vector2)transform.TransformDirection(Vector3.left) * this.ferrySpeed;
    6.             Direction clamps = CheckClamps(direction);
    7.             bool isLeftClamp = (clamps & Direction.DirectionLeft) == Direction.DirectionLeft;
    8.  
    9.             if (!isLeftClamp)
    10.             {
    11.                 this.rb2d.constraints = RigidbodyConstraints2D.None;
    12.                 rb2d.velocity = direction;
    13.             }
    14.             else
    15.             {
    16.                 Debug.Log("DO NOT MOVE LEFT ANYMORE");
    17.             }
    18.         }
    19.  
    20.         if (this.isRightArrowButtonPressing)
    21.         {
    22.             Vector2 direction = (Vector2)transform.TransformDirection(Vector3.right) * this.ferrySpeed;
    23.             Direction clamps = CheckClamps(direction);
    24.             bool isRightClamp = (clamps & Direction.DirectionRight) == Direction.DirectionRight;
    25.  
    26.             if (!isRightClamp)
    27.             {
    28.                 this.rb2d.constraints = RigidbodyConstraints2D.None;
    29.                 rb2d.velocity = direction;
    30.             }
    31.             else
    32.             {
    33.                 Debug.Log("DO NOT MOVE RIGHT ANYMORE");
    34.             }
    35.         }
    36.     }
    37.  
    38.     Direction CheckClamps(Vector2 direction)
    39.     {
    40.         Direction clamps = Direction.DirectionNone;
    41.         Transform camTransform = Camera.main.transform;
    42.  
    43.         float frustrumPositionLeftX = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, 0)).x;
    44.         float frustrumPositionRightX = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, 0)).x;
    45.  
    46.         Vector2 predictedPoint = new Vector2(this.transform.position.x, this.transform.position.y) + (direction * Time.fixedDeltaTime);
    47.  
    48.         if (predictedPoint.x <= frustrumPositionLeftX)
    49.         {
    50.             clamps |= Direction.DirectionLeft;
    51.         }
    52.  
    53.         if (predictedPoint.x >= frustrumPositionRightX)
    54.         {
    55.             clamps |= Direction.DirectionRight;
    56.         }
    57.  
    58.         return clamps;
    59.     }
    Thank you in advance
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    Couldn't you add a collider right outside of the camera view, so that the ship is stopped by it?
     
  3. sotrosh

    sotrosh

    Joined:
    Dec 25, 2014
    Posts:
    13
    if I add two vertical box colliders from the left and right of the camera the ship starts slightly shaking or rotating by Z order. Maybe you know how to prevent it.
     
    Last edited: Dec 4, 2018
  4. PapzZ

    PapzZ

    Joined:
    Jan 5, 2015
    Posts:
    30
    In the Inspector -> RigideBody2D -> Constraints(bottom) -> Freeze Rotation Z [ ] check the box ;)