1
\$\begingroup\$

I want to implement some sort of elastic field on my light blue magnetic field when a cylinder is colliding it, it worked except the lines won't stop bouncing. I am enabling the trigger collider on the cylinder since I want to be able to drag the cylinder up and down across the blue line, so I'm kinda stuck here..

Here's the demo, notice that the lines at the bottom of the cylinder is the effect that I wanted on the lines on top of the cylinder as well. I don't know why top lines behaving like that.

enter image description here

The curves that reacted to the cylinder is represented by the basic cube on each blue line which I added a collider and rigidbody to it.

enter image description here

And here is the code that I use to implement attract and repel effect like the first picture

private void Attract(Rigidbody rb)
{
    float Distance = Vector3.Distance(rb.transform.position, maxBounds.transform.position);
    float TDistance = Mathf.InverseLerp(MaxRange, 0f, Distance); // Give a decimal representing how far between 0 distance and max distance the object is.
    float strength = Mathf.Lerp(0f, MaxStrength, TDistance); // Use that decimal to work out how much strength the magnet should apply
    Vector3 FromObjectToMagnet = (maxBounds.transform.position - rb.transform.position).normalized; // Get the direction from the object to the magnet
    rb.AddForce(FromObjectToMagnet * strength, ForceMode.Force);// apply force to the object
}
private void Repulse(Rigidbody rb)
{   // This is exactly the same as Attract(), the direction is just reversed
    float Distance = Vector3.Distance(rb.transform.position, minBounds.transform.position);
    float TDistance = Mathf.InverseLerp(MaxRange, 0f, Distance); // Give a decimal representing how far between 0 distance and max distance the object is.
    float strength = Mathf.Lerp(0f, MaxStrength, TDistance); // Use that decimal to work out how much strength the magnet should apply
    Vector3 FromMagnetToObject = (rb.transform.position - minBounds.transform.position).normalized; // Get the direction from the object to the magnet
    rb.AddForce(FromMagnetToObject * strength, ForceMode.Force);// apply force to the object
}

I detect the colliders using OverlapCube method btw.

Any ideas? Thanks!

[Update] [enter image description here]3

The cube landed slowly to its original position, which is not the effect that I want.

\$\endgroup\$
  • 1
    \$\begingroup\$ Have you configured any drag on your curve cubes' rigidbodies? \$\endgroup\$ – DMGregory Nov 13 '18 at 12:49
  • \$\begingroup\$ @DMGregory I tried drag before, but it does not give me the effect that I wanted, I wanted the cube to instantly moves up and down, not slowly when using the drag. I edited the question to show the result. \$\endgroup\$ – Mira Nov 19 '18 at 0:51

Your Answer

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

Browse other questions tagged or ask your own question.