How to "catch" something that collides with a trigger area
Hi everyone!
I am working on a game where multiple balls are being thrown in the air. The player can catch one, run around with it, and then throw it.
I currently have a "Catch Area" game object with a capsule collider and a "Catch" script. In the code below, if a ball enters the trigger area, time slows down so you can catch it.
public class Catch : MonoBehaviour
{
public bool canCatch;
public Transform catchLocation;
public float slowDownFactor = 10f;
private void Start()
{
catchLocation = this.transform;
}
private void Update()
{
if (canCatch)
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
//SET THE BALL'S PARENT AS catchLocation
if (Input.GetKeyUp(KeyCode.Mouse0))
{
//SET THE BALL'S PARENT AS NULL
}
}
}
}
public void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Projectile"))
{
canCatch = true;
Time.timeScale = (1 / slowDownFactor);
Time.fixedDeltaTime = Time.timeScale * 0.02f;
}
}
public void OnTriggerExit(Collider other)
{
if (other.CompareTag("Projectile"))
{
canCatch = false;
Time.timeScale = 1f;
Time.fixedDeltaTime = Time.timeScale * 0.02f;
}
}
}
As the comments say in the code, I need to identify what game object has entered the trigger area, so I can set it as a child of the "Catch Area" game object.
Any good ideas on how I could do this?
Your answer
Follow this Question
Related Questions
Topdown 2D Colliders Not Working, help! 0 Answers
How do you add a child through code 2 Answers
turn off parent Gameobject based on a child game object that entered a Trigger? 2 Answers
Problem making object keep on RaycastHit point relative to another object 0 Answers
how do i make an instantiated object be a child of DontDestroyOnLoad object's child ? 2 Answers