Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

'Activate generators' script?

Discussion in 'Scripting' started by Deleted User, Jul 30, 2016.

  1. Deleted User

    Deleted User

    Guest

    Hello, everyone. As you guys may know, I'm making a survival horror game. Right now, I'm trying to make a script from one of my favorite Unity-powered games. I'm talking about Slender: The Arrival. In the game's third level, "Into The Abyss", you need to activate five generator trailers in order to power an elevator and escape. Does anyone know a good way how I can recreate this from the game as a script in Unity 5? If so, that would be a great help. Please and thank you so much! :)

    genpic.jpg
     
  2. SirNiklas

    SirNiklas

    Joined:
    Jun 7, 2015
    Posts:
    85
    First you need a script that catches input and shoots a raycast if the player is interacting, then check if the interacted object is a generator. Added to that a simple generator-script storing a value about its own state of activation. On top you need an elevator script that has a reference to all generator-components and opens itself (plays an animation for example) when the last generator is activated.

    Code (CSharp):
    1. [SerializeField]
    2. private float interactionDistance;
    3.  
    4. private void Update()
    5. {
    6.    CheckInput();
    7. }
    8.  
    9. private void CheckInput()
    10. {
    11.    if (Input.GetKeyDown(KeyCode.E))
    12.       TryActivateGenerator();
    13. }
    14.  
    15. private void TryActivateGenerator()
    16. {
    17.    RaycastHit hit;
    18.    // Seeks out a game object from the center of the screen (crosshair) to the direction the player is looking at
    19.    if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, interactionDistance))
    20.    {
    21.       if (hit.collider.CompareTag("Generator"))
    22.          hit.collider.GetComponent<Generator>().Activate();
    23.    }
    24. }
    This is an idea for a Generator class.
    Code (CSharp):
    1.  
    2. private bool activated; // Put a SerializeField-attribute here if you want to check the state of the generator in the inspector at runtime
    3. public bool Activated
    4. {
    5.    get { return activated; }
    6. }
    7.  
    8. [SerializeField]
    9. private Elevator connectedElevator; // Set this in the inspector
    10.  
    11. public void Activate()
    12. {
    13.    if (!activated && connectedElevator.ActivateGenerator(this))
    14.    {
    15.       activated = true;
    16.       // Show an information message on screen
    17.    }
    18. }
    Now, finally the elevator which opens on the final OnGeneratorActivated-call.
    Code (CSharp):
    1. [SerializeField]
    2. private Generator[] neededActivatedGeneratorsOrder;
    3. private int currentGeneratorOrderIndex = 0;
    4.  
    5. public bool ActivateGenerator(Generator generator)
    6. {
    7.    if (neededActivatedGeneratorsOrder[currentGeneratorOrderIndex] == generator) // Only the generator this index indicates can be activated right now
    8.    {
    9.       currentGeneratorOrderIndex++;
    10.       if (currentGeneratorOrder >= neededActivatedGeneratorsOrder.Length)
    11.          OpenDoor();
    12.  
    13.       return true;
    14.    }
    15.    else
    16.       return false;
    17. }
    18.  
    19. private void OpenDoor()
    20. {
    21.    animation.Play("Open");
    22.    // Show an informational message on the screen
    23. }

    This is the most structured option I can think of. I have not proof checked this in a code editor so it might be that there are minor mistakes.
    To improve this system, you can extend the interaction system in the first code snippet here to make it more generic for other kinds of interactions, for example a button in the elevator to let it drive up or down.
     
    gouthammannuru and Deleted User like this.
  3. Deleted User

    Deleted User

    Guest

    Thanks, SirNiklas! You really came to the rescue there and saved some time for my game. I truly appreciate it, dude!
     
    gouthammannuru likes this.
  4. Deleted User

    Deleted User

    Guest

    @SirNiklas, working on the script right now. Any idea how I can get the generator light to go from red to green upon activation, like it does in the game?
     
    gouthammannuru likes this.
  5. gouthammannuru

    gouthammannuru

    Joined:
    Sep 9, 2015
    Posts:
    16
    create 2 spot lights (red and green )
    use simple activate script on the lights when needed
     
    Deleted User likes this.
  6. SirNiklas

    SirNiklas

    Joined:
    Jun 7, 2015
    Posts:
    85
    @gouthammannuru This is no good advice. First, the amount of lights in a scene should be as low as possible. So using one point/spot light and setting its color property is more than enough.
    @DarkwoodProductions An even better way would be to attach a simple glow-shadered material onto the object of the small generator light and access the color property there, toggling it between green and red.
     
  7. SwagRogerCoolAid

    SwagRogerCoolAid

    Joined:
    Aug 17, 2016
    Posts:
    20
    I think burn heal might be needed. haha. SirNiklas answer is by far the best.