mirror of
https://github.com/nothke/quality-control.git
synced 2024-11-10 04:53:41 +00:00
35 lines
890 B
C#
35 lines
890 B
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class Magnet : MonoBehaviour
|
|
{
|
|
Rigidbody _rb;
|
|
Rigidbody rb { get { if (!_rb) _rb = GetComponent<Rigidbody>(); return _rb; } }
|
|
|
|
public float strength = 100;
|
|
|
|
private void OnTriggerStay(Collider other)
|
|
{
|
|
var otherRb = other.attachedRigidbody;
|
|
|
|
if (otherRb && otherRb.isKinematic == false)
|
|
{
|
|
if (!otherRb.GetComponent<Product>())
|
|
{
|
|
return;
|
|
}
|
|
|
|
Vector3 dir = otherRb.position - transform.position;
|
|
float dirSq = Vector3.SqrMagnitude(dir);
|
|
float forceMagnitude = strength * (1.0f / dirSq);
|
|
Vector3 force = dir.normalized * forceMagnitude;
|
|
|
|
otherRb.AddForce(-force);
|
|
|
|
if (rb)
|
|
rb.AddForce(force);
|
|
}
|
|
}
|
|
}
|