Bullets shooting the wrong way with AddForce

So my project right now has two “bases”, one on the right and one on the left. Each base has units going toward the opposite base and when they get in range of eachother, they are supposed to stop moving and attack eachother until all of one side is dead, then continue moving. Most of that works right now, but for some reason my attack script only works for the left side units, and not the right side ones. Specifically, the bullets that I instantiate go the wrong way when they are fired for the ones on the right side, but they work exactly as expected for the ones on the left… they both use the same script but the “tag” value is set to opposite units in the editor (so they will not attack themselves).

http://youtu.be/iTHr3D6QsL4 - a video demonstrating the problem.

Here is the script:

using UnityEngine;
using System.Collections;

public class AttackEnemy : MonoBehaviour {

	public GameObject bullet;
	public float bulletSpeed = 1.0f;
	public float fireRate = 1.0f;
	public float fireRadius = 5.0f;
	private	GameObject target = null;
	private UnitHealthControl enemyUnit;
	private float timeOfLastShot = 0.0f;
	public string tag;


	void FindTargets()
	{
		foreach(Collider col in Physics.OverlapSphere(transform.position, fireRadius))
		{
			if(col.tag == tag)
			{
				target = col.gameObject;
				enemyUnit = target.GetComponent<UnitHealthControl>();
				break;
			}
		}
		
		if(target != null)
		{
			gameObject.SendMessage ("isMoving", false);
			Debug.Log(gameObject.name + " has stopped moving");
			if(Time.time - fireRate > timeOfLastShot)
			{
				
				SpawnBullet();
				Debug.Log (gameObject.name + " is attacking " + target.name);
				timeOfLastShot = Time.time;
			}
		}
		else
		{
			if(enemyUnit.isAlive == false)
			{
				target = null;	
			}
			Debug.Log (gameObject.name + " has begun moving");
			gameObject.SendMessage ("isMoving", true);
		}
	}

	void Update()
	{

		FindTargets();
	}
	
	void SpawnBullet()
	{
		GameObject newBullet = Instantiate(bullet,transform.position, bullet.transform.rotation) as GameObject;
		newBullet.rigidbody.AddForce((target.transform.position - transform.position).normalized*bulletSpeed,ForceMode.VelocityChange);
	}

}

Anyone have a clue as to why this doesn’t work for both sets?

First, use transform.LookAt to rotate the players units so they are facing their target.
Then try using LerpAngle or SmoothDampAngle and give them little cube prefab guns just so you can see which way they’re currently facing, and wait until they rotate to face their target to actually fire.

Then set the rotation value in Instantiate to transform.rotation instead of bullet.transform.rotation, and use AddRelativeForce to move the bullet.

void SpawnBullet()
    {  
       GameObject newBullet = Instantiate (bullet,transform.position, transform.rotation) as GameObject;
       newBullet.rigidbody.AddRelativeForce(Vector3(0,0,bulletSpeed),ForceMode.VelocityChange);
    }

Also, any time you are using “transform” in Update() you should make a habit of putting “var myTransform : Transform” in the start of your script and then “myTransform = transform” in Awake() and using myTransform instead; it’s faster.