• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by KeithSeraph · Dec 09, 2018 at 12:57 PM · collisionphysicsrigidbodygunhingejoint

Hinge Joints Interfering with each other

I'm having a problem where my hinge joints on my game object keep colliding with each other even though they are completely separate. Here's a video that makes it easier to explain (Sorry My Mic Is Bad) : link text

And here's the entirety of the guns code, in case you can find a possible error:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GunScript : MonoBehaviour
 {
     public string gunName;
     public string ammoType;
     public float muzzleVelocity;
     public float bulletForce;
     public float roundPerMinute;
     public float weight;
 
     public enum GunType
     {
         Sniper, Rifle, Shotgun, Pistol
     }
     public GunType gunType;
 
     public enum FireMode
     {
         Full, Burst, Semi, Safety
     }
     public FireMode fireMode;
 
 
     [System.Serializable]
     public class GunComponents
     {
         public GameObject[] muzzleFlashes;
         public GameObject magazine;
         public GameObject shellEject;
         public GameObject bolt;
         public GameObject magDrop;
         public GameObject fireMode;
         public GameObject trigger;
         public GameObject casingPrefab;
         public GameObject bulletPrefab;
         public Transform bulletSpawnPoint;
         public Transform caseSpawn;
         
     }
     [SerializeField]
     public GunComponents gunComponents;
 
     [System.Serializable]
     public class GunSounds
     {
         public AudioSource gunSound;
         public AudioSource emptySound;
         public AudioSource reloadsound;
         public AudioSource pumpsound;
         public AudioSource boltsound;
     }
     [SerializeField]
     public GunSounds gunSounds;
 
 
     public bool triggerPulled;
     public bool emptyGun;
     public bool shoot;
     public bool canFire;
     public bool bolting;
 
     public int ammoInMagazine;
     public int ammoInChamber;
 
     public float fireRate;
     public float timer;
     float roundPerSecond;
     
 
     public float triggerPullRotation;
     public float triggerReleaseRotation;
 
     public float autoRotation;
 
     public float burstRotation;
 
     public float semiRotation;
 
     public float safetyRotation;
 
     public int shotFragments;
     public float spreadAngle;
 
     public Transform boltPositionClosed;
     public Transform boltPositionOpen;
 
     public Transform ejectPositionClosed;
     public Transform ejectPositionOpen;
 
     Rigidbody rb;
 
     bool opening;
     bool closing;
     
 
 
     void Awake()
     {
         rb = GetComponent<Rigidbody>();
         opening = false;
         closing = false;
     }
     
     void Update()
     {
         roundPerSecond = roundPerMinute / 60;
         fireRate = 1 / roundPerSecond;
 
         weight = rb.mass;
 
         HandleShoot();
 
         gunComponents.bolt.transform.localPosition = new Vector3(gunComponents.bolt.transform.localPosition.x,
             Mathf.Clamp(gunComponents.bolt.transform.localPosition.y, boltPositionClosed.transform.localPosition.y, boltPositionOpen.transform.localPosition.y),
             gunComponents.bolt.transform.localPosition.z);
 
         gunComponents.shellEject.transform.localPosition = new Vector3(gunComponents.shellEject.transform.localPosition.x,
             Mathf.Clamp(gunComponents.shellEject.transform.localPosition.y, ejectPositionClosed.transform.localPosition.y, ejectPositionOpen.transform.localPosition.y),
             gunComponents.shellEject.transform.localPosition.z);
         
 
         if (gunComponents.trigger.transform.localEulerAngles.x > triggerPullRotation)
         {
             triggerPulled = true;
             //Debug.Log("Trigger Pulled");    
         }
 
         if (gunComponents.trigger.transform.localEulerAngles.x < triggerReleaseRotation)
         {
             triggerPulled = false;
             canFire = true;
             //Debug.Log("Trigger Returned");    
         }
 
         if (Vector3.Distance(gunComponents.bolt.transform.localPosition, boltPositionOpen.transform.localPosition) < 0.01)
         {
             if (!bolting)
             {
                 StartCoroutine(WaitTillChamber());
                 //Debug.Log("Chamber Loading");
             }
         }
 
         if (gunType == GunType.Rifle || gunType == GunType.Pistol)
         {
             if (ammoInChamber == 0 && !bolting && !emptyGun)
             {
                 if (!opening)
                 {
                     StartCoroutine(LerpOpen());
                 }
             }
             if (bolting)
             {
                 if (!closing)
                 {
                     StartCoroutine(LerpClosed());
                 }
             }
         }
 
         if (triggerPulled && canFire && timer <= 0 && emptyGun)
         {
             DryFire();
         }
 
         if (ammoInMagazine == 0 && ammoInChamber == 0)
         {
             emptyGun = true;
         }
 
         if(Input.GetButtonDown("Fire1"))
         {
             HandleSwitch("Full");
         }
 
     }
 
     void HandleShoot()
     {
         switch (fireMode)
         {
             case FireMode.Safety:
                 timer -= Time.deltaTime;
                 break;
 
             case FireMode.Semi:
 
                 if (triggerPulled && canFire)
                 {
                     if (timer <= 0)
                     {
                         canFire = false;
 
                         if (ammoInChamber > 0)
                         {
                             Shoot();
                             shoot = true;
                         }
                         timer = fireRate;
                     }
                 }
                 else
                 {
                     shoot = false;
                 }
                 timer -= Time.deltaTime;
                 break;
 
             case FireMode.Burst:
 
                 if (triggerPulled && canFire)
                 {
                     if (timer <= 0)
                     {
                         canFire = false;
 
                         if (ammoInChamber > 0)
                         {
                             Shoot();
                             shoot = true;
 
                             if (ammoInMagazine > 0)
                             {
                                 Invoke("Shoot", fireRate);
 
                                 if (ammoInMagazine > 1)
                                 {
                                     Invoke("Shoot", fireRate * 2);
                                 }
                             }
                         }
                         timer = fireRate;
                     }
                 }
                 else
                 {
                     shoot = false;
                 }
                 timer -= Time.deltaTime;
                 break;
 
             case FireMode.Full:
 
                 if (triggerPulled)
                 {
                     if (timer <= 0)
                     {
                         canFire = false;
 
                         if (ammoInChamber > 0)
                         {
                             Shoot();
                             shoot = true;
                         }
 
                         timer = fireRate;
                     }
                 }
                 else
                 {
                     shoot = false;
                 }
                 timer -= Time.deltaTime;
                 break;
         }
     }
 
     public void HandleSwitch(string mode)
     {
         if(mode == "Full")
         {
             gunComponents.fireMode.transform.localEulerAngles = new Vector3(autoRotation, 0, 0);
             fireMode = FireMode.Full;
             //Debug.Log(gunComponents.fireMode.transform.localEulerAngles.x);
         }
         if (mode == "Burst")
         {
             gunComponents.fireMode.transform.localEulerAngles = new Vector3(burstRotation, 0, 0);
             fireMode = FireMode.Burst;
             //Debug.Log(gunComponents.fireMode.transform.localEulerAngles.x);
         }
         if (mode == "Semi")
         {
             gunComponents.fireMode.transform.localEulerAngles = new Vector3(semiRotation, 0, 0);
             fireMode = FireMode.Semi;
             //Debug.Log(gunComponents.fireMode.transform.localEulerAngles.x);
         }
         if (mode == "Safety")
         {
             gunComponents.fireMode.transform.localEulerAngles = new Vector3(safetyRotation, 0, 0);
             fireMode = FireMode.Safety;
             //Debug.Log(gunComponents.fireMode.transform.localEulerAngles.x);
         }
     }
 
     void PlayGunSound()
     {
         gunSounds.gunSound.Play();
     }
 
     void Shoot()
     {
         float randomSideRecoil = Random.Range(-bulletForce / 80, bulletForce / 80);
 
         transform.Rotate(transform.rotation.x - (bulletForce/20), transform.rotation.y, transform.rotation.z + (randomSideRecoil));
         //Debug.Log(randomSideRecoil);
 
 
         PlayGunSound();
 
         int i = Random.Range(0, gunComponents.muzzleFlashes.Length);
         Instantiate(gunComponents.muzzleFlashes[i], gunComponents.bulletSpawnPoint.position, gunComponents.bulletSpawnPoint.rotation);
         
         GameObject go = Instantiate(gunComponents.casingPrefab, gunComponents.caseSpawn.position, gunComponents.caseSpawn.rotation) as GameObject;
         Rigidbody rig = go.GetComponent<Rigidbody>();
         rig.AddForce(transform.right.normalized * 2 + Vector3.up * 1.3f, ForceMode.Impulse);
         rig.AddRelativeTorque(go.transform.right * 1.5f, ForceMode.Impulse);
         
 
         ammoInChamber = 0;
         //Debug.Log("Shot");
 
         if (gunType == GunType.Sniper || gunType == GunType.Rifle || gunType == GunType.Pistol)
         {
             GameObject bullet = Instantiate(gunComponents.bulletPrefab, gunComponents.bulletSpawnPoint.position, gunComponents.bulletSpawnPoint.rotation) as GameObject;
             Rigidbody shot = bullet.GetComponent<Rigidbody>();
             shot.AddForce(transform.forward * bulletForce);
         }
         if (gunType == GunType.Shotgun)
         {
             for (int s = 0; s < shotFragments; s++)
             {
 
                 Quaternion fireRotation = Quaternion.LookRotation(gunComponents.bulletSpawnPoint.transform.forward);
                 Quaternion randomRotation = Random.rotation;
 
                 fireRotation = Quaternion.RotateTowards(fireRotation, randomRotation, Random.Range(0.0f, spreadAngle));
 
                 GameObject bullet = Instantiate(gunComponents.bulletPrefab, gunComponents.bulletSpawnPoint.position, fireRotation) as GameObject;
                 Rigidbody shot = bullet.GetComponent<Rigidbody>();
                 shot.AddForce(transform.forward * bulletForce);
 
 
             }
         }
     }
 
     void DryFire()
     {
         gunSounds.emptySound.Play();
     }
 
     IEnumerator LerpOpen()
     {
         opening = true;
         closing = false;
 
         float t = 0;
 
         while (t < fireRate * 0.5f)
 
         {
             t += Time.deltaTime;
 
             gunComponents.bolt.transform.localPosition = Vector3.Lerp(boltPositionClosed.transform.localPosition, boltPositionOpen.transform.localPosition, t / (fireRate * 0.5f));
             gunComponents.shellEject.transform.localPosition = Vector3.Lerp(ejectPositionClosed.transform.localPosition, ejectPositionOpen.transform.localPosition, t / (fireRate * 0.5f));
             //Debug.Log("Opening");
             //Debug.Log(t);
             yield return null;
         }
     }
 
     IEnumerator LerpClosed()
     {
         opening = false;
         closing = true;
 
         float p = 0;
 
         while (p < fireRate * 0.5f)
 
         {
             p += Time.deltaTime;
 
             gunComponents.bolt.transform.localPosition = Vector3.Lerp(boltPositionOpen.transform.localPosition, boltPositionClosed.transform.localPosition, p / (fireRate * 0.5f));
             gunComponents.shellEject.transform.localPosition = Vector3.Lerp(ejectPositionOpen.transform.localPosition, ejectPositionClosed.transform.localPosition, p / (fireRate * 0.5f));
             //Debug.Log("Closing");
             //Debug.Log(p);
             yield return null;
         }
     }
 
     IEnumerator WaitTillChamber()
     {
         bolting = true;
         yield return new WaitForSeconds(fireRate * 0.5f);
         bolting = false;
         opening = false;
         closing = false;
 
         if (ammoInMagazine > 0)
         {
             ammoInChamber = 1;
             ammoInMagazine--;
         }
         //Debug.Log("Chamber Loaded");
     }
 }
 

I appreciate any possible assistance. Also, if there's a way to stop a rigidbodys momentum that'd be great. :)

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

206 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple grab locations on 1 game object?,MULTIPLE GRAB LOCATIONS on 1 game object? 0 Answers

Player maintains velocity even when parent collides 2 Answers

How do i get OnTriggerStay2D to work? 1 Answer

Collision causes weird effects 2 Answers

Collisions between two rigidbody's and scripting collisions problem 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges