Child object using Origin (0, 0, 0) as its forward direction

Hi all,

I have a child empty object which is supposed to face the forward direction. However, it does so as it moves towards the Origin (0, 0, 0) direction, then as it moves passed the Origin, it flips its Z-axis (by 180 deg) and faces in the opposite direction as it moves away.

For example, if starting on the left of the Origin, it will move forward fine (with its Z-axis pointing to the right), then flip its Z to face to the left as it passes it, and vice-versa if approaching from the right.

I’m using transform.forward in the script attached to the child object. I have also tried transform.TransformDirection(Vector3.forward), new Vector3 (0, 0, 1). I then make the object LookAt() this direction.

The relevant part of the script is below.

Any ideas why its doing this?

Thanks in advance.

    // Hold the direction the bullet will fire - Enemy or straight ahead
    public Vector3 bulletDirection = Vector3.zero;

    // Variable to hold the direction of the enemy, assigned to the Instantiation, etc
    private Vector3 enemyDirection = Vector3.zero;

    // Used to reference the Player object
    private GameObject playerObj;

    // Used to reference the LockOnTriggerObject
    private GameObject lockOnTriggerObj;

    public float ShootDelay = 1.0F;

    public float Timer = 0.0F;

    private Vector3 fwd = Vector3.zero;

    void Start()
    {
        // Find the Player Object 
        playerObj = GameObject.Find("Player");

        // Find the LockOnTrigger object
        lockOnTriggerObj = GameObject.Find("PlayerLockOnTrigger");

        // Direction is along the Z-axis - straight ahead
        fwd = transform.TransformDirection(Vector3.forward);
    }

    void FixedUpdate()
    {
        Firing();
    }

    void Firing()
    {
        // If enemy targeted, set bulletDirection to position of the enemy
        if (lockOnTriggerObj.GetComponent<ClosestEnemy>().EnemyTargetted)
        {
            Debug.Log("Inside if-");

            // Access the ClosestEnemy script and its EnemyPosition variable
            enemyDirection = lockOnTriggerObj.GetComponent<ClosestEnemy>().EnemyPosition;
            // Assign the value in the variable to the bullet's direction
            bulletDirection = enemyDirection;
        }
    
        // If no enemy targeted, shoot straight ahead
        if (lockOnTriggerObj.GetComponent<ClosestEnemy>().EnemyTargetted == false)
        {
            // Straight ahead - original direction
            // bulletDirection = fwd;

            // bulletDirection = gameObject.transform.TransformDirection(0, 0, -1)

            // transform.TransformDirection(Vector3.forward)
            
            bulletDirection = new Vector3 (0, 0, 1);
        }

        // Make the BulletSpawner_R object face the assigned direction
        transform.LookAt(bulletDirection);

        // Fire the bullet
        newBullet.AddForce(transform.forward * bulletPower);

So I think this is it:

You need to change your code to do the following:

When you are targeting an enemy - take the current transform position away from the enemies position. Then use Quaternion.LookRotation to set the transforms rotation to point in that direction then add your force like you are now.

bulletDirection = lockOnTriggerObj.GetComponent<ClosestEnemy>().EnemyPosition - transform.position; 
...
transform.rotation = Quaternion.LookRotation(bulletDirection)

Then remove the transform.LookAt();

Now the code that fiddles with forward will work the same way as the code that targets the enemy. The problem you have is that at the moment they are both trying to do a different thing. One is using positions and the other directions.