Arrow Looking in the wrong rotation

I have this code to aim at the target

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TrackickSystem : MonoBehaviour
{
    public float speed = 3.0f;
    public GameObject m_target = null;
    Vector3 m_lastKnownPosition = Vector3.zero;
    Quaternion m_lookAtRotation;

    // Update is called once per frame
    void Update()
    {
        if (m_target)
        {
            if (m_lastKnownPosition != m_target.transform.position)
            {
                m_lastKnownPosition = m_target.transform.position;
                m_lookAtRotation = Quaternion.LookRotation(m_lastKnownPosition - transform.position);
            }

            if (transform.rotation != m_lookAtRotation)
            {
                transform.rotation = Quaternion.RotateTowards(transform.rotation, m_lookAtRotation, speed * Time.deltaTime);
            }
        }
    }

    bool SetTarget(GameObject target)
    {
        if (!target)
        {
            return false;
        }

        m_target = target;

        return true;
    }
}

And this code to shoot at the target

    IEnumerator Fire()
    {   
        //instantiate the bullet as gameobject 

        GameObject Bullet = Instantiate(Bullets, Gun.gameObject.transform.position, Gun.gameObject.transform.rotation* Quaternion.Euler (0f, 0f, 0f)) as GameObject; 
        Rigidbody RigidBodyBullet = Bullet.GetComponent<Rigidbody>();
        RigidBodyBullet.AddForce(RigidBodyBullet.transform.forward * 5);
        aslan.Play();// fire sound

        //wait until the time between shots to complete and fire again.
        IsShoot = false;
        
        Destroy(Bullet, BulletLifeTime);
        yield return new WaitForSeconds(TimeBtwShots);
        IsShoot = true;
    }

It does shoot at the target but the arrow that the turret shoots looks in the wrong rotation. I need to turn it -90 degrees in X rotation. Please help how to do it.

If you have these in all the guns separately, you should be able to just make the bullets a child of the gun in question: Instantiate(Bullets, transform); or if needed you can put Gun.gameObject.transform instead. That or you can just remove the * Quaternion.Euler (0f, 0f, 0f) part if you want it to not be a child of anything. Any of that, or you could literally just say Bullet.transform.eulerAngles -= new Vector3(-90, 0, 0); You know, either one.

Though ignoring the question for a bit, there are some changes I would make to your code in general to make it easier to understand and tweak (no offense):

  1. You can get the Transform rather than the GameObject to remove the need to type transform over and over.
  2. The default value of most variables is null, so putting public GameObject m_target = null; is redundant.
  3. Keep in mind you can use Transform.eulerAngles whenever you want to change the rotation of an object based on Vector3s instead of Quaternions, also making it easier to see how to fix what went wrong since this is what is displayed in the editor.
  4. Instantiate already creates returns the GameObject created so saying Instantiate(Bullets) as GameObject; is redundant.
  5. If at any point you want to have the player or something else disable shooting for a period, you should probably set firing to the place in which the coroutine called or just otherwise not put it in a coroutine, and you should probably have a variable as the coroutine’s input instead of using a set variable, allowing it to be changed without having to change it back.

Hope this helped!

It looks like your arrow forward direction is set wrong by 90 degrees.
because of Quaternion.LookRotation is working based on forward direction and your arrow forward is not correct.

  • I recommend setting the proper forward direction of your arrow mesh in Blender.
  • Or making the mesh (3d object) child of the arrow and setting it properly, basically rotating the child by 90 degrees.
  • Or modify your code by adding extra 90 degrees always when you apply the rotation.