Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Player character getting out of bed - best way to use colliders?

Discussion in '2D' started by karakori, Jul 1, 2020.

  1. karakori

    karakori

    Joined:
    Feb 2, 2020
    Posts:
    7
    There is a probably a very simple and elegant solution for this, but I'm having trouble figuring out exactly how I'd go about doing it.

    Basically:



    Scene fades in, character is in bed and opens their eyes. If player presses left or right they can move out of the bed (I don't want them to be able to exit from the top or bottom of the bed). Once they're out of the bed, I need to enable a box collider around the bed so they can't get back in.

    Enabling the box collider seems simple enough, but is there a more elegant way to restrict player movement to left/right when they wake up other than making two box or edge colliders at the top and bottom of the bed, and then disabling them once the player is out of bed? And when would I disable them? After the player moves x amount of distance from the bed? Is there a way to detect when the player is no longer occupying a certain space?

    Any help is appreciated.
     
  2. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    Your idea is ok. This just is another one with Action delegates

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. public class Movement : MonoBehaviour
    5. {
    6.     private static Action action;
    7.     private static Vector3 lastSleep;
    8.  
    9.     void Awake()
    10.     {
    11.         action = Sleep;
    12.     }
    13.  
    14.     private void Update()
    15.     {
    16.         action();
    17.     }
    18.  
    19.     private void Move()
    20.     {
    21.         print("I'm moving");
    22.         //your standard movement script
    23.     }
    24.  
    25.     private void Die()
    26.     {
    27.         print("I'm dead");
    28.     }
    29.  
    30.     private void Sleep()
    31.     {
    32.         lastSleep = transform.position;
    33.         print("I'm sleeping");
    34.         if (Input.GetAxisRaw("Horizontal") == 1)
    35.         {
    36.             action = WakeUpRight;                
    37.         }
    38.         else if(Input.GetAxisRaw("Horizontal") == -1)
    39.         {
    40.             action = WakeUpLeft;
    41.         }
    42.     }
    43.  
    44.     private void WakeUp(Vector3 direction)
    45.     {
    46.         print("I'm waking up");
    47.         transform.position = Vector3.MoveTowards(transform.position, lastSleep + direction, Time.deltaTime);
    48.         if (transform.position == lastSleep + direction)
    49.             action = Move;
    50.     }
    51.  
    52.     private void WakeUpRight() => WakeUp(Vector3.right);
    53.     private void WakeUpLeft() => WakeUp(Vector3.left);
    54.  
    55.     private void OnCollisionEnter2D(Collision2D collision)
    56.     {
    57.         action = Die;
    58.     }
    59. }
     
    karakori likes this.
  3. karakori

    karakori

    Joined:
    Feb 2, 2020
    Posts:
    7
    Very interesting - I'm not familiar with delegates yet so I'm going to have to do some reading on the subject. Thank you for sharing your code!