- Home /
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