0
\$\begingroup\$

So I created a magnetic effect, where objects are "sucked" into my player. It works great.

Now I want those same objects to get a little Impulse force when they spawn. This has somehow reversed my magnetic effect? The objects are now pushed away from the player...

All I did was add this Line in Start():

_rb.AddForce(_forwardThrust, _upThrust, _forwardThrust, ForceMode.Impulse);

Full Script here:

public class RandomMagneticForce : MonoBehaviour
{
    private bool _inside;
    private Transform _magnet;
    private Rigidbody _rb;
    private SphereCollider _sphere;
    float radius = 5f;
    [SerializeField]
    private float _delay;
    [SerializeField]
    private float _force = 70;
    [SerializeField]
    private float _upThrust, _forwardThrust;

    void Start()
    {
        _magnet = GameManager.instance.ActivePlayer.gameObject.transform;
        _rb = GetComponent<Rigidbody>();
        _sphere = GetComponent<SphereCollider>();
        _inside = false;
        _rb.AddForce(Random.Range(-_forwardThrust, _forwardThrust), _upThrust, Random.Range(-_forwardThrust, _forwardThrust), ForceMode.Impulse);
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag(Constants.TAG_PLAYER))
        {
            _inside = true;
        }
    }
    void OnTriggerExit(Collider other)
    {
        if (other.gameObject.CompareTag(Constants.TAG_PLAYER))
        {
            _inside = false;
        }
    }

    private void Update()
    {
        if (_delay > 0)
            _delay -= Time.deltaTime;

        if (_inside)
        {
            if (_delay <= 0)
            {
                Vector3 magnetField = _magnet.position - transform.position;
                float index = (radius - magnetField.magnitude) / radius;
                _rb.AddForce(_force * magnetField * index);
            }
        }
    }
}
\$\endgroup\$
2
\$\begingroup\$

Here's what your attraction force's signed magnitude looks like as we vary the distance to the magnet, using positive to show force toward the magnet and negative for force away:

Graph of attraction versus distance, showing peak attraction at 2.5 and negative attraction (repulsion) past 5

So it looks like you might just be missing a Max or a check to ensure your index (really an intensity) doesn't go below zero and turn your attractive force into a repulsive force for objects outside of radius:

Vector3 magnetField = _magnet.position - transform.position;
float index = (radius - magnetField.magnitude) / radius;
if(index > 0f)
    _rb.AddForce(_force * magnetField * index);

If you were previously spawning these objects only inside the radius of 5, you would have seen only the attractive part of this curve. Now, by adding some initial movement, they sometimes drift outside the radius into the repulsion field, making it look like the new AddForce call changed the behaviour of the attraction when it really just revealed behaviour that was already there.

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.