Ok… so I have two scripts.
One that lets me push, and pull my object:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Gravity : MonoBehaviour
{
public bool reverseForce = false;
public float strength = 50f;
public float strengthExponent = 1.1f;
public bool scaleStrengthOnMass = false;
public float range = 50f;
public string targetTag = "";
public List<Rigidbody> objectsInRange;
private Transform _transform;
private Rigidbody _rigidbody;
void Start()
{
_transform = transform;
if (GetComponent<Rigidbody>() != null)
{
_rigidbody = GetComponent<Rigidbody>();
}
else if (scaleStrengthOnMass)
{
Debug.LogError("Gravity - ScaleStrengthOnMass is on but rigidbody missing");
}
SphereCollider c = gameObject.AddComponent<SphereCollider>();
c.radius = range;
c.isTrigger = true;
}
void OnTriggerEnter(Collider c)
{
if (c.tag == targetTag)
{
if (c.GetComponent<Rigidbody>() != null)
{
objectsInRange.Add(c.GetComponent<Rigidbody>());
}
else
{
Debug.LogWarning("Gravity - Tagged object has no rigidbody");
}
}
}
void OnTriggerExit(Collider c)
{
if (c.tag == targetTag)
{
if (c.GetComponent<Rigidbody>() != null)
{
objectsInRange.Remove(c.GetComponent<Rigidbody>());
}
}
}
void Update()
{
{
{
if (!Input.GetMouseButton(0)) return;
float forceMultiplier;
Vector3 forceDirection;
foreach (Rigidbody a in objectsInRange)
{
forceMultiplier = (-strength / Mathf.Pow(Mathf.Max(Vector3.Distance(_transform.position, a.position), 1f), strengthExponent));
if (scaleStrengthOnMass)
{
if (GetComponent<Rigidbody>() != null)
{
forceMultiplier *= _rigidbody.mass;
}
}
if (reverseForce || Input.GetMouseButton(1))
{
forceMultiplier *= -1;
}
forceDirection = (a.position - _transform.position).normalized;
a.AddForce(forceDirection * forceMultiplier);
}
}
// void OnDrawGizmosSelected();
{
//Gizmos.color = (new Color(0f,0f,1f));
//Gizmos.DrawWireSphere(_transform.position,range);
}
}
}
}
and my script that I need to trigger an event when it enters my trigger:
using UnityEngine;
using System.Collections;
public class Switch : MonoBehaviour
{
GameObject Ball;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter(Collider coll)
{
if (coll.gameObject.tag == "Ball")
Debug.Log("Ball Entered! :)");
}
}
Problem being when I have the Gravity script enabled it automatically triggers the event which is a problem. I have no idea why it’s doing this. Any ideas?