I need help with Pick Ups
I'm trying to make a feature that causes a pick up to be magnetized to the player when the player gets close enough. This would be to get rid of the annoyance of just missing the pickup by a slight amount, without having an unrealistically big hit box. How could I go about doing this?
Note: The game I'm making is programmed in C#.
Answer by Jessespike · Apr 21, 2016 at 09:30 PM
Have you tried Vector3.Distance? You can change the scale of the object when the distance falls below a value. If you want an item to grow bigger as you get closer, you can use Vector3.Lerp as well.
That's not what I'm going for though. I want the pickup to move towards the player when the distance between the pick up and the player falls below a certain value.
Also I'm sorry if i was unclear initially. I just realized that I typed "magnified" when I meant to say "magnetized."
Answer by Soraphis · Apr 22, 2016 at 05:18 PM
@Jessespike might have missunterstood you but generally his solution would be correct:
check the players distance to all pickup-objects if the distance is smaller than X move the object a bit closer to the player (velocity can be calculated by distance (further away > slower distance)) if the distance is smaller than Y, pick it up.
to move the object closer to the player you can use Lerp, thats correct too.
instead of checking every single pickable every frame you could add a collider around the player (add it as a child gameobject). make it a trigger, set it to never sleep.
add a script to the pickables, where you use "OnTriggerStay", to check if its in the "magnetsphere" and move it a bit closer.
Your answer

Follow this Question
Related Questions
Pickup objects pass through other objects 0 Answers
Help with consuming an object on key press 1 Answer
How to make a coin spin(not a 3d mesh) 1 Answer
OnCollisionEnter2D not being called 1 Answer