Hey there guys, I am having a bit of an issue with the directions of force being produced by a rigidbody.
What I am trying to achieve is a realistic gun shooting mechanic.
So how this works is, when you shoot, a bullet casing is instantiated, being shot out the side of the gun.
Also a bullet gets instantiated and gets shot forward by the given force, this works great in theory and great as a prop, as can be seen in the below youtube clip.
https://www.youtube.com/watch?v=Lx9qW0pUzQI
but when the gun is attached to a FirstPersonCharacter both objects (bullet and casing) get forced in the same direction (example, the casings shoot out to the right hand side of the gun, but if i spin the character 180 degrees, they are now being shot out to the left side of the gun. so this added force is relative to the worlds direction, not the direction of its parent object (the gun)... how can this be achieved to use the direction of the parent (the gun) to send the force from and not the world's directions?
EDIT:
My apologies, I had forgotten to add my piece of code, the gun handles all the shooting things, but once instantiated the objects take over from there own scripts.
Bullet Casing Script:
using UnityEngine;
public class ejectCase : MonoBehaviour {
float thrust = 250;
Rigidbody rig;
// Use this for initialization
void Start () {
rig = this.GetComponent();
rig.AddForce(transform.right * thrust);
}
// Update is called once per frame
void Update () {
}
}
Bullet Script:
using UnityEngine;
public class pistolBulletScript : MonoBehaviour {
float thrust = 1000;
Rigidbody rig;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("testHit"))
{
Debug.Log("Bullet has hit Target, Health Removed from Target, Bullet Removed");
Destroy(this.gameObject);
}
}
// Use this for initialization
void Start () {
rig = this.GetComponent();
rig.AddForce(-transform.forward * thrust);
}
// Update is called once per frame
void Update () {
}
}
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 Vector3 caseEjector;
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;
caseEjector = casePos.transform.position;
shootLoc = shootPos.transform.position;
Instantiate(casing, caseEjector, Quaternion.Euler(0, 90, 180));
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;
}
}
}
}
PS. all the directions are jacked up due to forward not actually being the tip of the bullet ect.
and for some reason all my buttons to make the text bolt and italic ect are missing, so i cannot click 'code' to add it correctly.
↧