mirror of
https://github.com/nothke/quality-control.git
synced 2025-08-30 07:23:43 +00:00
Magnet
This commit is contained in:
26
Assets/Scripts/Magnet.cs
Normal file
26
Assets/Scripts/Magnet.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
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)
|
||||
{
|
||||
Vector3 dir = otherRb.position - rb.position;
|
||||
float dirSq = Vector3.SqrMagnitude(dir);
|
||||
float force = strength * (1.0f / dirSq);
|
||||
|
||||
otherRb.AddForce(-dir * force);
|
||||
rb.AddForce(dir * force);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user