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

Cover System

Discussion in 'Scripting' started by Johker, Oct 27, 2010.

  1. Johker

    Johker

    Joined:
    Jun 24, 2010
    Posts:
    24
    I am trying to create a cover system for a 3D Isometric view game. At the moment I have a Cube Mesh (scale 5, 2 ,5) setup with a Mesh Collider that is not a Trigger. I then added a Box Collider (size 1.5, 1, 1.5) to the Cube Mesh and made it a trigger. The cover object(s) will always be aligned according to the global X and Z axes.

    My character is just a capsule mesh with a Character Controller and my PlayerMovementController script.

    I have the following script which is attached to the Cube/Cover:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [RequireComponent(typeof(BoxCollider))]
    6. public class CoverTrigger : MonoBehaviour
    7. {
    8.     private PlayerMovementController controller;
    9.     private bool attachedToCover = false;
    10.    
    11.         private void OnTriggerStay(Collider collider)
    12.     {
    13.         if (Input.GetButton("Fire1")  attachedToCover)
    14.         {
    15.             attachedToCover = false;
    16.         }
    17.         else if (Input.GetButton("Fire1")  !attachedToCover)
    18.         {
    19.             attachedToCover = true;
    20.         }      
    21.        
    22.         if (attachedToCover)
    23.         {
    24.             controller = collider.GetComponent<PlayerMovementController>();
    25.             //controller.TakeCover();      
    26.         }
    27.     }
    28. }
    29.  
    The code is really really simple at this stage.

    Things I still need to do is:

    1) To "attach" the Player Character to the cover surface and to restrict his movements to the cover object surface, and up to the different ends. I was thinking of using bounding edges but I'm not sure if this is the right way to tackle this problem. Any ideas regarding this?

    2) I also need to figure out on which side of the cover surface the Player Character is and to attach him the appropriate side.

    Any ideas, suggestions or help?