Search Unity

Magnetism

Discussion in 'Physics' started by AGENTofSHADOWS3, Apr 20, 2017.

  1. AGENTofSHADOWS3

    AGENTofSHADOWS3

    Joined:
    Apr 20, 2017
    Posts:
    7
    So I'm creating a game where you can magnetize certain objects using a ray gun. I've gotten a script together that can list which objects aren't charged (neutral), which objects are positively charged, and which objects are negatively charged. What I'm trying to figure out now is how to make the objects repel each other or attract each other based on their charges (e.g., two objects of the same charge will repel each other with more force if they are closer together, two objects of opposite charges will attract each other with more force when they get closer, etc.). I'm not the best at the physics scripting, though. Does anyone have any suggestions as to how I could accomplish this? (I can try working with Java, but I'd prefer C#.)
     
  2. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    AGENTofSHADOWS3 likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
  4. Tarball

    Tarball

    Joined:
    Oct 16, 2016
    Posts:
    166
    Why not just assign an integer to each object? For example, you could assign 1 to objects you wish to repel, -1 to objects to attract, and 0 to objects with no effect. Then, multiply that by your applied force vector for the desired effect.
     
  5. AGENTofSHADOWS3

    AGENTofSHADOWS3

    Joined:
    Apr 20, 2017
    Posts:
    7
    Thanks, guys. I was able to get the desired effect through the following script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Magnetized : MonoBehaviour
    {

    public int Charge;
    public List<GameObject> magObjectsList = new List<GameObject> ();
    public List<GameObject> neutralObjectsList = new List<GameObject> ();
    public List<GameObject> positiveObjectsList = new List<GameObject> ();
    public List<GameObject> negativeObjectsList = new List<GameObject> ();
    GameObject player;

    public float radius = 5.0f;
    public float power = 10.0f;

    void Start ()
    {
    if (this.name != "Switch" && this.name != "Red Cube" && this.name != "Blue Cube") {
    this.Charge = 0;
    } else {
    return;
    }

    player = GameObject.Find ("Player");
    }

    public void AddPosCharge ()
    {
    this.Charge = 1;
    }

    public void AddNegCharge ()
    {
    this.Charge = 2;
    }

    public void RevertCharge ()
    {
    this.Charge = 0;
    }

    void FixedUpdate ()
    {
    if (Input.GetKeyDown (KeyCode.R) && this.name != "Switch" && this.name != "Red Cube" && this.name != "Blue Cube") {
    RevertCharge ();
    }
    magObjectsList.Clear ();
    GameObject[] allMagObjects = GameObject.FindGameObjectsWithTag ("Metal");
    foreach (GameObject cube in allMagObjects) {
    magObjectsList.Add (cube);
    }

    if (magObjectsList.Count > 0) {
    positiveObjectsList.Clear ();
    negativeObjectsList.Clear ();
    neutralObjectsList.Clear ();
    foreach (GameObject cube in magObjectsList) {
    if (cube.GetComponent<Magnetized> ().Charge.ToString () == "0") {
    neutralObjectsList.Add (cube);
    }
    if (cube.GetComponent<Magnetized> ().Charge.ToString () == "1") {
    positiveObjectsList.Add (cube);
    }
    if (cube.GetComponent<Magnetized> ().Charge.ToString () == "2") {
    negativeObjectsList.Add (cube);
    }
    }
    }
    Magnetism ();
    }

    public void Magnetism ()
    {
    if (this.Charge.ToString () == "0") {

    }
    if (this.Charge.ToString () == "1" && this.name != "Switch") {
    Vector3 posPosition = this.transform.position;
    if (positiveObjectsList.Count > 1) {
    foreach (GameObject cube in positiveObjectsList) {
    string tag = cube.tag;
    Rigidbody rb = cube.GetComponent<Rigidbody> ();

    if (rb != null) {
    if (tag != "Player") {
    rb.AddExplosionForce (power, posPosition, radius, -0.1f);
    }
    if (tag == "Player") {
    rb.AddExplosionForce (0.0f, posPosition, radius, -0.1f);
    }
    }
    }
    }
    if (negativeObjectsList.Count > 0) {
    foreach (GameObject cube in negativeObjectsList) {
    string tag = cube.tag;
    Rigidbody rb = cube.GetComponent<Rigidbody> ();

    if (rb != null) {
    if (tag != "Player") {
    rb.AddExplosionForce (-power, posPosition, radius);
    }
    if (tag == "Player") {
    rb.AddExplosionForce (0.0f, posPosition, radius);
    }
    }
    }
    }
    }
    if (this.Charge.ToString () == "2" && this.name != "Switch") {
    Vector3 negPosition = this.transform.position;
    if (positiveObjectsList.Count > 0) {
    foreach (GameObject cube in positiveObjectsList) {
    string tag = cube.tag;
    Rigidbody rb = cube.GetComponent<Rigidbody> ();

    if (rb != null) {
    if (tag != "Player") {
    rb.AddExplosionForce (-power, negPosition, radius);
    }
    if (tag == "Player") {
    rb.AddExplosionForce (0.0f, negPosition, radius);
    }
    }
    }
    }
    if (negativeObjectsList.Count > 1) {
    foreach (GameObject cube in negativeObjectsList) {
    string tag = cube.tag;
    Rigidbody rb = cube.GetComponent<Rigidbody> ();

    if (rb != null) {
    if (tag != "Player") {
    rb.AddExplosionForce (power, negPosition, radius, -0.1f);
    }
    if (tag == "Player") {
    rb.AddExplosionForce (0.0f, negPosition, radius, -0.1f);
    }
    }
    }
    }
    }
    }
    }