- Home /
how to create Magnetic repulsion ?
Hello Every One,
I need to create the magnet object with repulsion. I created the magnet it is pulling the object if that object is with in the radius. Normally Magnet has a two poles North and south now my object is sticking the north side if i rotate the object it does not sticking that side its changing but i want to do if i rotate the object as well as the object also rotate that corresponding side. Please help me to do this.Here is the code:
var MagnetLayer : LayerMask;
var FieldPosition : Vector3;
var FieldRadius : float;
var FieldForce : float;
var rot : float = 2.0f;
function FixedUpdate () {
transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * 5, 0.0f, Input.GetAxis("Vertical") * Time.deltaTime * 5);
if(Input.GetKey(KeyCode.R)){
transform.Rotate(Vector3.up, Space.Self);
}
var colliders : Collider[];
var rigidbody : Rigidbody;
colliders=Physics.OverlapSphere (transform.position + FieldPosition, FieldRadius, MagnetLayer);
for(var collider in colliders){
rigidbody = collider.rigidbody;
if(rigidbody== null )
{
continue;
}
rigidbody.AddExplosionForce (FieldForce * -1, transform.position + FieldPosition, FieldRadius);
}
}
function OnDrawGizmosSelected()
{
Gizmos.color= Color.red;
Gizmos.DrawWireSphere (transform.position, FieldRadius);
}
Please do not create duplicates of the same question. -_-
Your answer
Follow this Question
Related Questions
how to create the magnet similar to the real magnet ? 1 Answer
Create a leaf with it's natural motion acting upon gravity? 2 Answers
Moving a rigidbody onto exact mouse position using rigidbody.MovePosition? 1 Answer
ConfigurableJoint Anchors and nested RigidBodies 2 Answers
Why is my object tilting when moving? 2 Answers