How to fire a bullet from a gun?
I followed the tut. I added two guns to my player prefab. I am now trying to get the bullet to fire from the bulletTransform attached to the gun and not from the tsTransform position. When I do this the bullet for player 1 is correct and fires from the correct gun and in the correct direction. However player 2's bullet fires from player 1's gun and in the correct direction that player 2 is facing. I am struggling with getting the correct transform. I had it working in Unity but using transform.Find which is not an option for tsTransform. Here is the code I put in.
public override void OnSyncedStart() { StateTracker.AddTracking(this); // get the location from our transform for bullets Rweapons = GameObject.FindGameObjectsWithTag("RWeapons"); foreach (GameObject Rweapon in Rweapons) { if (localOwner.Id == owner.Id) { rWeaponTrans = Rweapon.transform.Find("BulletTransform").gameObject; } } Lweapons = GameObject.FindGameObjectsWithTag("LWeapons"); foreach (GameObject Lweapon in Lweapons) { if (localOwner.Id == owner.Id) { lWeaponTrans = Lweapon.transform.Find("BulletTransform").gameObject; } } }I then take the rWaponTrans x,y and z and put them into a TSVector and then in TrueSyncManager.SyncedInstantiate I use that TSVector for position.
0
Best Answer
-
You need to first find the unit related to that player.
e.g. if you player class is called PlayerClass
void FireGun(int WhichPlayerID)
{
var players = GameObject.FindObjectsOfType < PlayerClass > ();
foreach ( var player in players )
{
if ( player.id == WhichPlayerID )
{
Transform bulletOrigin = player.transform.Find("BulletTransform");
}
... etc
Right now you're finding *all* of the players and just grabbing one at random to fire from.
}6
Answers
-
You need to first find the unit related to that player.
e.g. if you player class is called PlayerClass
void FireGun(int WhichPlayerID)
{
var players = GameObject.FindObjectsOfType < PlayerClass > ();
foreach ( var player in players )
{
if ( player.id == WhichPlayerID )
{
Transform bulletOrigin = player.transform.Find("BulletTransform");
}
... etc
Right now you're finding *all* of the players and just grabbing one at random to fire from.
}6