Firing projectile from Transform. What am I doing wrong?
I've made a simple RTS style setup just using basic objects from Unity. I have a Unit, a weapon and projectiles prefabs. The weapon is attached to the unit via a transform. I can move the unit around and the weapon follows as it's supposed to. However when I go to fire the projectile, the projectile continues to spawn from the center of where the Unit starts. No matter what I do the bullets just spawn where the unit used to be and go on their way.
I've attached a transform to the weapon at the exit point I want the projectiles to come from. I've made sure to drag and drop it in the inspector. I've used Debug.Log to discover that the weapon thinks it's at (0, 0, 0) but I don't understand why. I've attached the code for the Unit and the Weapon below.
I want the ammo projectiles to spawn from the end of the weapon even if the Unit has moved and it should fire in the direction the Unit is facing.
Any help would be greatly appreciated. I've found many answers here but this is the firs time I've had to post to get help. Hopefully I've posted this in the right place and formatted/given the right information.
Unit Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Unit : MonoBehaviour
{
// Properties
public string unitName;
public int hp;
public Weapon weapon;
private Weapon gun;
public Transform weaponSlot;
private MeshRenderer rend;
private AudioSource _audio;
public AudioClip clip_selected;
public AudioClip clip_affirm;
public bool selected = false;
private NavMeshAgent navAgent;
// Start is called before the first frame update
void Start()
{
Vector3 ws = weaponSlot.position;
rend = GetComponentInChildren<MeshRenderer>();
_audio = GetComponent<AudioSource>();
gun = Instantiate(weapon, ws, Quaternion.Euler(90, 0, 0));
gun.transform.SetParent(weaponSlot);
navAgent = GetComponent<NavMeshAgent>();
rend.material.color = Color.black;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonUp(0))
{
RaycastHit hit = new RaycastHit();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit) && hit.transform.tag == "Marine")
{
_audio.PlayOneShot(clip_selected);
selected = true;
}
}
if (Input.GetMouseButtonDown(0) && selected == true)
{
RaycastHit hit = new RaycastHit();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit) && selected == true)
{
_audio.PlayOneShot(clip_affirm);
navAgent.destination = hit.point;
}
}
if (Input.GetMouseButtonDown(1))
{
selected = false;
}
if(Input.GetKeyDown(KeyCode.Space))
{
gun.Fire();
}
}
private void OnMouseEnter()
{
rend.material.color = Color.white;
}
private void OnMouseExit()
{
rend.material.color = Color.black;
}
}
Weapon Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
// Properties
public string weaponName;
public int rateOfFire;
public int damage;
public AudioClip fire;
public Ammo ammo;
public Transform gunExit;
Vector3 v_gunExit;
AudioSource _audio;
// Start is called before the first frame update
void Start()
{
_audio = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
}
public void Fire()
{
Debug.Log(this.transform.localPosition);
Instantiate(ammo, gunExit.localPosition, Quaternion.identity);
}
}
Answer by graydogstudios · Apr 26 at 02:12 AM
Line 35: Instantiate(ammo, gunExit.localPosition, Quaternion.identity);
And earlier you have:
public Transform gunExit
This is going to be (0,0,0) assuming weapon is attached to player. Thus it will come out of the player position.
Other than that, Id have to see the xyz in inspector. This is because:
gun.transform.SetParent(weaponSlot);
So gun would be a child of weaponSlot, which is a child of CharacteR?
Thanks for the feedback. It doesn't come out of the player but from (0,0,0) on the 'map'. It just spawns in mid air no matter where the player is. The gun is a child of weaponSlot which is a child of character yes. I got it to fire from the player location but not the correct direction by passing the gun.transform.position to the Fire() function but it still just shoots straight up or down AND for some reason the character 'jitters' when I fire. Its supposed to come out of the end of the gun but doesn't.