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

InputHandler central class VS distributed input handling

Discussion in 'Scripting' started by dunloip, Aug 21, 2018.

  1. dunloip

    dunloip

    Joined:
    Aug 8, 2018
    Posts:
    15
    It seems clear to me that having input handling all over your code is not a good idea in principle. Therefore it would be good to create an Input component to handle all that. However I find myself in a situation trying to make this component work:
    I have some BoxCollider2D components contained in some Game Objects and I want the Input component to notice when these colliders are clicked by the mouse. However, the classes that contain those BoxCollider2D are the ones that are going to implement the changes later. So, is it OK in this case to implement the input handling in that very same classes?
    Otherwise I can't figure out how to elegantly detect a click from the InputHandler in the BoxColliders contained in classes all over the scene.
     
  2. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    You can use a markup interface. YOu use a implement a interface to "tag" your behavior for mouse click interaction. Here is a similar exmaple from our game, you mark it for interaction with your "hand" (its a VR game)

    Code (CSharp):
    1.    public interface IPhysicalHandCollidable
    2.     {
    3.         void OnCollide(NVRHand hand, Collision col);
    4.     }
    5.  
    6.     public class PhysicalControllerCollisionListener : MonoBehaviour
    7.     {
    8.         private NVRHand hand;
    9.  
    10.         public void Init(NVRHand hand)
    11.         {
    12.             this.hand = hand;
    13.         }
    14.  
    15.         public void OnCollisionEnter(Collision collision)
    16.         {
    17.             var collidable = NVRInteractables.GetInteractable(collision.collider) as IPhysicalHandCollidable;
    18.  
    19.             if (collidable != null)
    20.             {
    21.                 collidable.OnCollide(hand, collision);
    22.             }
    23.         }      
    24.     }
    Used like

    Code (CSharp):
    1. public class RollerDelayedBlowbackSlide : RotatingBolt, IPhysicalHandCollidable
    2.     {
    3.         ...
    4.  
    5.         public void OnCollide(NVRHand hand, Collision col)
    6.         {
    7.             if(IsLockedBack)
    8.             {
    9.                 var force = Vector3.Project(col.relativeVelocity, SlamDirection.up);
    10.  
    11.                 var angle = Vector3.Angle(SlamDirection.up, col.relativeVelocity);
    12.  
    13.                 if (angle < 60 && force.magnitude > 0.25f)
    14.                 {
    15.                     ReturnSlide();
    16.                     hand.ForceGhost();
    17.                 }            
    18.             }
    19.         }      
    20.     }