Making object point in direction it is moving in

I am a new unity developer a I was trying to make and object point in the direction it was moving in I have tried transform.foward but it does not turn my object and makes it invisible for some reason. I have tried to figure something out myself but it does not work. I wold appreaciate yor help. Here is my code for the object:

public class Bird : MonoBehaviour
{
    // Start is called before the first frame update
    public Rigidbody2D myRigidbody;
    public float FlapStrength;
    public LogicsManager logic;
    bool birdIsAlive = true;
    public AudioSource myAudioSource;
    public Vector3 place;
    public PlaceHolder Holder;

    void Start()
    {
        logic = GameObject.FindGameObjectWithTag("Logics").GetComponent<LogicsManager>();
        place = GameObject.FindGameObjectWithTag("Place").GetComponent<Transform>().position;
        Holder = GameObject.FindGameObjectWithTag("Place").GetComponent<PlaceHolder>();

        myRigidbody.velocity = Vector2.up * FlapStrength;
        myAudioSource.Play();
    }

    // Update is called once per frame
    void Update()
    {
        if ((Input.GetKeyDown(KeyCode.W) == true || Input.GetKeyDown(KeyCode.UpArrow) == true) && birdIsAlive == true)
        {

            myRigidbody.velocity = Vector2.up * FlapStrength;
            myAudioSource.Play();
            myRigidbody.gravityScale = 3;
            transform.localScale = new Vector3(0.5168F, 0.5168F, 0);

        }
        if ((Input.GetKeyDown(KeyCode.S) == true || Input.GetKeyDown(KeyCode.DownArrow) == true) && birdIsAlive == true)
        {
            myRigidbody.velocity = Vector2.down * FlapStrength;
            myAudioSource.Play();
            myRigidbody.gravityScale = -3;
            transform.localScale = new Vector3(0.5168F, -0.5186F, 0);
        }



        if (transform.position.y < -11.25 || transform.position.y > 1)
        {
            logic.gameOver();
            BirdDie();
        }
        StartCoroutine(Point());
    }
    public IEnumerator Point()
    {
        Holder.placeGo();
        yield return new WaitForSeconds(0.1f);
        transform.LookAt(place);
        transform.Rotate(0, 0, 180);
        transform.rotation = new Quaternion(0, 0, transform.rotation.z, 0);

    }
    void OnCollisionEnter2D(Collision2D collision)
    {
        logic.gameOver();
        BirdDie();
    }

    public void BirdDie()
    {
        birdIsAlive = false;
    }
}

and here is the code for the object with tag “Place”

public class PlaceHolder : MonoBehaviour
{
    // Start is called before the first frame update
    public Vector3 place ;
    void Start()
    {
        place = GameObject.Find("Bird").GetComponent<Transform>().position;
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void placeGo()
    {
        place = GameObject.Find("Bird").GetComponent<Transform>().position;
        transform.position = place;
        
    }
    
        
    
}

sorry in advance for the long code and thank you for your help. tell me if you need me to send you more scripts.

Everything down here is generated by microsoft copilot :wink::

It looks like you’re trying to make your bird object point in the direction it’s moving using transform.LookAt(). However, there are a few issues with your current approach. The transform.LookAt() function is designed for 3D space and may not work as expected with 2D objects like your bird, which is using a Rigidbody2D. Additionally, setting the transform.rotation directly after using LookAt() will override the rotation set by LookAt().

Here’s a revised approach that should work for a 2D setup:

C#

void Update()
{
    // ... existing code ...

    // Call the function to point the bird in the direction of movement
    PointInDirectionOfMovement();
}

void PointInDirectionOfMovement()
{
    if (myRigidbody.velocity != Vector2.zero)
    {
        float angle = Mathf.Atan2(myRigidbody.velocity.y, myRigidbody.velocity.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    }
}

AI-generated code. Review and use carefully. More info on FAQ.

This code calculates the angle of the velocity vector and then creates a rotation from that angle around the Z-axis, which is the forward axis for 2D objects in Unity. The Quaternion.AngleAxis function is used to create the appropriate rotation.

Remove the StartCoroutine(Point()) call from your Update() method and the entire Point() coroutine, as they are no longer needed with this new method.

Remember to test the changes and adjust the FlapStrength or the angle calculation if the bird points in the wrong direction. This should make your bird object point in the direction it’s moving without making it invisible.

Thank you so much for th quick response!!!