Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

I need a checkpoint and GetComponent script please

Discussion in 'Scripting' started by KingsleyD, Feb 18, 2020.

  1. KingsleyD

    KingsleyD

    Joined:
    Jan 12, 2020
    Posts:
    7
    I'm making a 3d horror game and need a checkpoint script where when you collide with a monster it would teleport you to the checkpoint. I also need a GetComponent script where when you go into an area a script titled "Flashlight" is set false. Thanks, I'm new and don't know these things.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Welcome! I recommend you start here: Learning

    Luckily there are some great tutorials (also on Youtube) for doing almost EXACTLY the above things.
     
    TheDevloper, exiguous and bobisgod234 like this.
  3. TheDevloper

    TheDevloper

    Joined:
    Apr 30, 2019
    Posts:
    69
    Hi there, here is my approach, so first of all
    1- Create an empty game object + add a box colllider to it and check the is trigger option
    2- Duplicate that empty game object and assign this script to it

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CheckPoint : MonoBehaviour
    4. {
    5.     public bool IsPassed { get; private set; }
    6.  
    7.     private void OnTriggerEnter2D(Collider2D collision)
    8.     {
    9.         var player = collision.GetComponent<PlayerMovement>();
    10.         if(player != null)
    11.         {
    12.             IsPassed = true;
    13.         }
    14.     }
    15. }
    16.  
    3- Try to edit the script, add the script movement of your monster etc.
    4- Create a second empty game object and name it as a checkPointManager, and drag and drop all other checkpoints as children.
    5- Add this second script to checkPointManager
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Linq;
    3.  
    4. public class CheckPointManager : MonoBehaviour
    5. {
    6.     private CheckPoint[] CheckPoint;
    7.  
    8.     void Start()
    9.     {
    10.         CheckPoint = GetComponentsInChildren<CheckPoint>();
    11.     }
    12.  
    13.     public CheckPoint LastCheckPoint()
    14.     {
    15.         return CheckPoint.Last(T => T.IsPassed);
    16.     }
    17.  
    18. }
    19.  
    6- Now go to your game manager script and assign this 3rd script where we will assign the position of the checkpoint to the player.

    Code (CSharp):
    1.     private void SendPlayerToChecPoint()
    2.     {
    3.         var checkPointManager = FindObjectOfType<CheckPointManager>();
    4.         var checkPoint = checkPointManager.LastCheckPoint();
    5.         var player = FindObjectOfType<PlayerMovement>();
    6.  
    7.         player.transform.position = checkPoint.transform.position;
    8.     }
    7- Enjoy
     
    KingsleyD likes this.
  4. KingsleyD

    KingsleyD

    Joined:
    Jan 12, 2020
    Posts:
    7
    Thank you so much! This really helps me!
     
    TheDevloper likes this.
  5. KingsleyD

    KingsleyD

    Joined:
    Jan 12, 2020
    Posts:
    7
    One thing I have to ask is how to you get to the checkpoint once you hit an object?
     
  6. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    u can create empty on checkpoint and then move player to this spot?
     
    TheDevloper likes this.
  7. TheDevloper

    TheDevloper

    Joined:
    Apr 30, 2019
    Posts:
    69
    Just create an empty object and duplicate them, then place those empty objects wherever you like.