Hey there guys, I am having an issue with the directions of force being produced by a rigidbody. I am trying to make a gun that shoots a bullet casing out the right of the gun, but when happens is it always shoots to the right of the world space, so if i spin 180° the bullet casing is now being shot of of the left side of the gun, i have provided a detailed video clip to both explain what im trying to achieve and show what has previously been suggested, but does not work.
https://youtu.be/43mQRbDgMEM
My Coding
Bullet Casing Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class caseEjector : MonoBehaviour {
float thrust = 250;
float flickThrust = 0.1f;
public GameObject bulletCase;
Rigidbody rig;
public void shoot()
{
GameObject cap = Instantiate(bulletCase, transform.position, Quaternion.Euler(0, 0, 0));
cap.transform.parent = gameObject.transform;
rig = cap.GetComponent();
rig.AddRelativeForce(new Vector3(1, 0.1f, 0).normalized * thrust);
cap.transform.parent = null;
}
}
Gun Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestShoot : MonoBehaviour {
public bool canShoot;
public bool reloading;
public float bulletClip = 17;
float origClip;
float shotTimer;
float startRTimer;
float reloadTimer = 1.5f;
Animation anim;
public AudioClip shotFired;
public AudioClip reloadSound;
public AudioClip emptyClip;
AudioSource aud;
public GameObject caseEjectorGO;
public Transform casing;
public Transform casePos;
public Vector3 shootLoc;
public Transform bullet;
public Transform shootPos;
void Start () {
origClip = bulletClip;
startRTimer = reloadTimer;
aud = this.GetComponent();
anim = this.GetComponent();
canShoot = true;
reloading = false;
}
void Update () {
shotTimer += Time.deltaTime;
if(shotTimer <= 0.4f)
{
canShoot = false;
}
else
{
canShoot = true;
}
if (canShoot && bulletClip > 0)
{
if (Input.GetButtonDown("Shoot"))
{
anim.Play("APCFire");
aud.PlayOneShot(shotFired);
bulletClip -= 1;
shotTimer = 0;
shootLoc = shootPos.transform.position;
caseEjectorGO.GetComponent().shoot();
Instantiate(bullet, shootLoc, Quaternion.Euler(0, 90, 0));
}
}
if (bulletClip <= 0 && canShoot)
{
if (Input.GetButtonDown("Shoot"))
{
aud.PlayOneShot(emptyClip);
shotTimer = 0;
}
}
if (Input.GetButtonDown("Reload"))
{
aud.PlayOneShot(reloadSound);
canShoot = false;
reloading = true;
anim.Play("APCReload");
}
if (reloading)
{
reloadTimer -= Time.deltaTime;
if(reloadTimer <= 0)
{
bulletClip = origClip;
reloading = false;
canShoot = true;
reloadTimer = startRTimer;
}
}
}
}
↧