This commit is contained in:
nothke
2024-08-17 15:13:39 +02:00
parent fa1e2ddbf4
commit 601c99ebf6
6 changed files with 1427 additions and 0 deletions

26
Assets/Scripts/Magnet.cs Normal file
View 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);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b236863c86ffb73498f0262aaf900afd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: