- Home /
How can i get the attracted objects to stick to the bottom of the magnet and not rotate around the attraction point?
Hello, I am currently working on a game with a crane/magnet system and needed some help. Basically When I want to move something around the object is attracted to the magnet. But it doesn't stick to the bottom of the magnet and only moves around the magnet point that has the script and the objects move through the mess of the actual magnet model.
This is the code for the magnet:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SC_RigidbodyMagnet : MonoBehaviour { public float magnetForce = 100;
List<Rigidbody> caughtRigidbodies = new List<Rigidbody>();
void FixedUpdate() { if (Input.GetKey("space")) { for (int i = 0; i < caughtRigidbodies.Count; i++) { caughtRigidbodies[i].velocity = (transform.position - (caughtRigidbodies[i].transform.position + caughtRigidbodies[i].centerOfMass)) magnetForce Time.deltaTime; } } }
void OnTriggerEnter(Collider other)
{
if (other.GetComponent<Rigidbody>())
{
Rigidbody r = other.GetComponent<Rigidbody>();
if (!caughtRigidbodies.Contains(r))
{
//Add Rigidbody
caughtRigidbodies.Add(r);
}
}
}
void OnTriggerExit(Collider other)
{
if (other.GetComponent<Rigidbody>())
{
Rigidbody r = other.GetComponent<Rigidbody>();
if (caughtRigidbodies.Contains(r))
{
//Remove Rigidbody
caughtRigidbodies.Remove(r);
}
}
}
}