Search Unity

Question Gameobject not moving with character until I manually change any constraint during the game?

Discussion in 'Scripting' started by Yettie123, May 28, 2023.

  1. Yettie123

    Yettie123

    Joined:
    Jun 20, 2017
    Posts:
    79
    I wrote a pickup script for my player and when I pickup the object it gets appended to my equip position and enables the shooting script and I am able to shoot with it. Furthermore, the gun moves as I rotate the player but as soon as I try and move forward, back, or sideways the gun stays in place. This fixes however when I manually change any value or checked box in the rigidbody component regardless of what it is, it seemingly makes no sense. It seems to only fix when the rigidbody component is altered/updated in any way, and altering it through a script doesn't work either I tried, only works when I alter with my mouse during the game. Here is my ray pickup code.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Ray : MonoBehaviour
    6. {
    7.  
    8.     public Camera cam;
    9.  
    10.     public float pickUpRange = 4f;
    11.     public float dropForwardForce;
    12.     public float dropUpwardForce;
    13.  
    14.     public GameObject currWeapon;
    15.     public Transform player, equipPosition, sideArmPosition, dropForceVector;
    16.  
    17.     public bool isEquipped;
    18.  
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.  
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         if (Input.GetKeyDown(KeyCode.F))
    29.         {
    30.             RaycastHit hit;
    31.             if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, pickUpRange))
    32.             {
    33.                 currWeapon = hit.transform.gameObject;
    34.                 if (hit.collider != null)
    35.                 {
    36.                     Debug.Log(currWeapon.name);
    37.                     PickUp();
    38.                 }
    39.             }
    40.         } else if (Input.GetKeyDown(KeyCode.Q))
    41.         {
    42.             Drop();
    43.             currWeapon = null;
    44.         }
    45.     }
    46.  
    47.     void PickUp()
    48.     {
    49.         isEquipped = true;
    50.  
    51.         // make object a child of the camera and move to position
    52.         currWeapon.transform.SetParent(equipPosition);
    53.         currWeapon.transform.localPosition = Vector3.zero;
    54.         currWeapon.transform.localRotation = Quaternion.Euler(Vector3.zero);
    55.         currWeapon.transform.localScale = Vector3.one;
    56.  
    57.         // make Rigidbody kinematic and BoxCollider a trigger
    58.         currWeapon.GetComponent<Rigidbody>().isKinematic = true;
    59.         currWeapon.GetComponent<BoxCollider>().isTrigger = true;
    60.  
    61.         // enable gunScript on object (if object is a weapon will be done later)
    62.         currWeapon.GetComponent<GunShoot>().enabled = true;
    63.  
    64.         // disable sphere gravity on object when picked up
    65.         currWeapon.GetComponent<GravityBody>().enabled = false;
    66.     }
    67.  
    68.     void Drop()
    69.     {
    70.         isEquipped = false;
    71.  
    72.         // set parent of object to null
    73.         currWeapon.transform.SetParent(null);
    74.  
    75.         // make Rigidbody not kinematic and BoxCollider not a trigger
    76.         currWeapon.GetComponent<Rigidbody>().isKinematic = false;
    77.         currWeapon.GetComponent<BoxCollider>().isTrigger = false;
    78.  
    79.         // gun carries momentum of player when dropped
    80.         currWeapon.GetComponent<Rigidbody>().velocity = GetComponent<Rigidbody>().velocity;
    81.  
    82.         // add force to dropped object
    83.         currWeapon.GetComponent<Rigidbody>().AddForce(dropForceVector.forward * dropForwardForce, ForceMode.Impulse);
    84.         currWeapon.GetComponent<Rigidbody>().AddForce(dropForceVector.up * dropUpwardForce, ForceMode.Impulse);
    85.  
    86.         // disbale gunScript on object
    87.         currWeapon.GetComponent<GunShoot>().enabled = false;
    88.  
    89.         // enable sphere gravity on object when dropped
    90.         currWeapon.GetComponent<GravityBody>().enabled = true;
    91.     }
    92. }
    93.  
     
    Last edited: May 28, 2023