Working magnet and crane AI

This commit is contained in:
nothke
2024-08-17 16:13:26 +02:00
parent 601c99ebf6
commit b2d8ac944c
14 changed files with 1764 additions and 3176 deletions

View File

@@ -0,0 +1,71 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CranePickDrop : MonoBehaviour
{
public SlidingCrane crane;
public Magnet magnet;
public Transform dropTarget;
public Rigidbody handlingBody;
public float magnetStrength;
public enum State
{
Idle,
Catching,
Tansporting,
Finished,
}
public State state;
public void OnTriggerEnterSignalReceived(EnterTriggerSender sender)
{
var otherRb = sender.triggeredCollider.attachedRigidbody;
if (otherRb && otherRb.isKinematic == false && state == State.Idle)
{
handlingBody = otherRb;
state = State.Catching;
}
}
public void OnTriggerExitSignalReceived(EnterTriggerSender sender)
{
var otherRb = sender.triggeredCollider;
}
void Start()
{
magnetStrength = magnet.strength;
}
void Update()
{
if (state == State.Catching)
{
Debug.Assert(handlingBody, this);
magnet.strength = magnetStrength;
crane.testTgt = handlingBody.transform;
if (Vector3.Distance(handlingBody.position, magnet.transform.position) < 1f)
{
crane.testTgt = dropTarget;
state = State.Tansporting;
}
}
else if (state == State.Tansporting)
{
if (magnet.IsCloseToPlanar(dropTarget))
{
magnet.strength = 0;
state = State.Idle;
}
}
}
}

View File

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

View File

@@ -0,0 +1,26 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class EnterTriggerSender : MonoBehaviour
{
public UnityEvent onEnterEvent;
public UnityEvent onExitEvent;
public Collider triggeredCollider;
private void OnTriggerEnter(Collider other)
{
Debug.Log("Entered: " + other.name);
triggeredCollider = other;
onEnterEvent.Invoke();
}
private void OnTriggerExit(Collider other)
{
Debug.Log("Exited: " + other.name);
triggeredCollider = other;
onExitEvent.Invoke();
}
}

View File

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

View File

@@ -13,14 +13,16 @@ public class Magnet : MonoBehaviour
{
var otherRb = other.attachedRigidbody;
if(otherRb)
if (otherRb)
{
Vector3 dir = otherRb.position - rb.position;
Vector3 dir = otherRb.position - transform.position;
float dirSq = Vector3.SqrMagnitude(dir);
float force = strength * (1.0f / dirSq);
otherRb.AddForce(-dir * force);
rb.AddForce(dir * force);
if (rb)
rb.AddForce(dir * force);
}
}
}

View File

@@ -17,15 +17,10 @@ public class SlidingCrane : MonoBehaviour
public Transform testTgt;
public Vector3 target;
void Start()
{
yMotion.AccelerateTo(1);
xMotion.AccelerateTo(1);
}
void Update()
{
target = testTgt.position;
if (testTgt)
target = testTgt.position;
Vector3 localTarget = transform.InverseTransformPoint(target);
xMotion.Update(Time.deltaTime);
@@ -34,7 +29,6 @@ public class SlidingCrane : MonoBehaviour
Vector2 targetPlanar = new Vector2(localTarget.x, localTarget.z);
float xTgt = Mathf.InverseLerp(-xRange, xRange, targetPlanar.x);
Debug.Log(xTgt);
float yTgt = Mathf.InverseLerp(0, yRange, targetPlanar.y);
xMotion.AccelerateTo(xTgt);

View File

@@ -0,0 +1,21 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class DistanceExtension
{
public static Vector2 Planar(this Vector3 vector)
{
return new Vector2(vector.x, vector.z);
}
public static bool IsCloseTo(this Component c1, Component c2, float range = 1f)
{
return Vector3.Distance(c1.transform.position, c2.transform.position) < range;
}
public static bool IsCloseToPlanar(this Component c1, Component c2, float range = 1f)
{
return Vector2.Distance(c1.transform.position.Planar(), c2.transform.position.Planar()) < range;
}
}

View File

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