Search Unity

Rigidbody moves two GameObjects instead of one

Discussion in 'Scripting' started by DoggyBoggy, Mar 26, 2020.

  1. DoggyBoggy

    DoggyBoggy

    Joined:
    Jan 19, 2018
    Posts:
    20
    I have a 3d game where im trying to make it so that you can grab objects yet when I have this script attactched to two gameobjects in my scene they both come to the reticle and since there is no other script affecting their motion and ive debugged the heck out of this one im assuming theres something inherent in the functions im using that affects all gameobject's current positions. Anyway heres the code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class ThrowableObject : MonoBehaviour
    5. {
    6.     public float throwForce = 100;
    7.     public static bool touchingWall = false;
    8.     public static bool closestViewedObj = false;
    9.     public static bool beingCarried = false;
    10.     public float dragSpeed = 2f;
    11.     public static Vector3 lastPos;
    12.     public static float distance;
    13.     public static Ray ray;
    14.     public static Image reticle;
    15.     public static Color32 defaultColor;
    16.     public static RaycastHit hit;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         reticle = GameObject.FindGameObjectWithTag("Reticle").GetComponent<Image>();
    22.         defaultColor = reticle.color;
    23.         lastPos = transform.position;
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         if (!beingCarried)
    30.         {
    31.             closestViewedObj = false;
    32.             ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    33.  
    34.             if (Physics.Raycast(ray, out hit, 2.5f, 1 << 9))
    35.             {
    36.                 closestViewedObj = hit.transform.position == transform.position ? true : false;
    37.             }
    38.  
    39.             if (closestViewedObj)
    40.             {
    41.                 reticle.color = new Color32(120, 231, 120, 255);
    42.                 if (Input.GetButtonUp("Use"))
    43.                 {
    44.                     GetComponent<Rigidbody>().useGravity = false;
    45.                     distance = Vector3.Distance(Camera.main.transform.position, transform.position);
    46.                     beingCarried = true;
    47.                 }
    48.             }
    49.             else if (!Physics.Raycast(ray, out hit, 2.5f, 1 << 9))
    50.             {
    51.                 reticle.color = defaultColor;
    52.             }
    53.         }
    54.        
    55.         else
    56.         {
    57.             GetComponent<Rigidbody>().velocity = (Camera.main.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, distance)) - transform.position) * dragSpeed;
    58.  
    59.             if (Input.GetMouseButtonDown(0))
    60.             {
    61.                 GetComponent<Rigidbody>().useGravity = true;
    62.                 transform.parent = null;
    63.                 beingCarried = false;
    64.                 GetComponent<Rigidbody>().AddForce(Camera.main.transform.forward * throwForce);
    65.             }
    66.             else if (Input.GetButtonDown("Use"))
    67.             {
    68.                 GetComponent<Rigidbody>().useGravity = true;
    69.                 transform.parent = null;
    70.                 beingCarried = false;
    71.             }
    72.         }
    73.  
    74.     }
    75.  
    76. }
    77.  
    78.  
    But yea, Any help is appreciated! im kinda a noob so go easy on me lol. Thanks! =)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    First, remove all the
    static
    declarations. That's probably going to account for 99.9% of your problems.
     
    Joe-Censored likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    To expand on @Kurt-Dekker when you set a variable to "static" it means it is part of the class itself instead of an instance of the class. That effectively shares the exact same variable between all instances of the class. Usually that isn't what you want for the majority of the variables in a MonoBehaviour.
     
    Kurt-Dekker likes this.