I made a magnet effect but it works incorrectly.
There are balls in my game scene. If the distance between the magnet object and balls were less than 2.5, then magnet effects will work.
it’s my script for the magnet effect.
function Start () {
}
function Update () {
var pos : Vector3 = transform.position;
var other : GameObject = GameObject.FindWithTag(“Ball”);
- var dist : float;
var ran : float;
ran = Random.Range(20,25);
10.
dist = Vector3.Distance(pos,other.transform.position);
if(dist<2.5)
{
15. other.rigidbody.AddForce(Vector3(ran,0,ran),ForceMode.Force);
}
}
But, actually it doesn’t. Only one ball is affected by magnet effect.
All balls in game scene should be affected by magnet.
What’s wrong with my script?
Plz, anybody teach it to me.