- Home /
Magnetic object to make coins move to object
I'm trying to make an object that is moving at various speeds to be magnetic and if it gets close enough to a coin it will suck it in towards the object.
What I have right now works kind of:
distance = Vector3.Distance(transform.position, junk.transform.position);
if(distance <= 35 && !destroySequence) {
destroySequence = true;
}
if(destroySequence && !moving) {
moving = true;
iTween.MoveTo(transform.gameObject, iTween.Hash("x", transform.position.x, "y", junk.transform.position.y, "z", junk.transform.position.z,"time", 1.0f, "oncomplete", "destroyCoin"));
}
It moves the coin to my object which is sometimes moving a very fast rates and the object will move past the position that the coin is suppose to move to so the object is usually in front of the coins when they reach the position.
So what I tried is if the coins hit the object, that it destroys the coin. But what I found is the coin almost never hits the object. I've been doing a lot of searching on the internet and can't find what I am looking for. I also tried lowering the time on the iTween but that makes it so quick you can't even see it, and it still has that same issue above.
If you don't understand think of Temple Run and the magnet power-up and how it sucks in the coins. Something like that.
Thanks.
Answer by fafase · Nov 29, 2012 at 06:55 AM
Make your coin a Rigidbody and try AddForce to the coin. So you need to try first because I did not... The idea is that you have a trigger sphere around the coin. When the magnet gets in, you start calculation. You already have a reference to the transform of your magnet.
What you do is get the magnitude of the vector between the coin and the magnet. Use a linear equation to define an index. Then you pass all the required variable to AddForce. This way, as you get closer to the magnet, magnetField.magnitude gets smaller->index gets bigger->the force gets bigger. You might have to change the force value as well
bool inside;
Transform magnet;
float radius = 5f;
float force = 100f;
void Start(){
magnet = GameObject.Find("Magnet").GetComponent<Transform>();
inside = false;
}
void OnTriggerEnter(Collider other){
if(other.gameObject.tag =="Magnet"){
inside =true;
}
void OnTriggerExit(Collider other){
if(other.gameObject.tag =="Magnet"){
inside =false;
}
if(inside){
Vector3 magnetField = magnet.position- transform.position;
float index = (radius-magnetField.magnitude)/radius;
AddForce(force*magnetField*index);
}
I seem to have another issue now. It works with this when it goes at relatively slow speeds. But when my object that is the magnet starts picking up speed, the coins miss the magnet and just circle it and picking up speed like crazy. I tried doing if the distance < 5 (an example) or whatever that it deletes it. But it seems to just go flying past it and ignore that. Any ideas on how to get this working on high velocity objects? Thanks though!
is if (inside) in the FixedUpdate? Also, I would rather increase the force ins$$anonymous$$d of altering distance.
Yes it is inside FixedUpdate and I did increase the force and it still happens to do it although not as often. It would just circle the object faster and faster ins$$anonymous$$d of actually going to it. Thanks for all the help so far though.
Ok, what you describe actually makes sense to me. $$anonymous$$aybe you want to add some drag and angular drag. Simply said, drag is the air resistance and angular drag is the friction. Your environment might be somehow ideal with no friction at all, so nothing restrains the forces adding together and your coin keeps on accumulating forces.
Air resistance / drag increases with the velocity of the object so the faster your coin goes, the more opposite force it gets.
Friction / angular drag is a little similar until a certain critical velocity depending on the system. Think of your car, at low speed, you brake and the car stops properly, but at high speed, the wheels lose grip and you start skidding.
So my tip would be to start adding values to drag and angular drag and you might see your coin acting in a more physical way.
You could look there http://unitygems.com/mistakes1/. the video shows in action what those variables do.
Answer by Maulik2208 · Nov 29, 2012 at 06:34 AM
Try to associate it with *time.deltatime just like we are doing in Rotation like stuff.......and i am not so sure But your problem will be solved by that.....
Your answer
![](https://koobas.hobune.stream/wayback/20220613084656im_/https://answers.unity.com/themes/thub/images/avi.jpg)