How to make missile face the way it's moving

I’m busing making a game and I want the missiles to face the way they’re moving.

I’ve seen other questions answered on this topic, but they all set the velocity of the game object using RigidBody’s, but I use transform.position.

Is there any way to make it face the way it’s moving without using the physics engine in unity?

using UnityEngine;

public class MissileController : MonoBehaviour
{   
  private Vector3 previousPosition; 
  
  private void Start()
  {
      // Initialize the previousPosition to the starting position of the missile.
      previousPosition = transform.position;
  }
  
  private void Update()
  {
      // Calculate the direction vector between the previous and current position.
      Vector3 direction = transform.position - previousPosition;
  
      // Check if the missile is moving (magnitude greater than a small threshold).
      if (direction.sqrMagnitude > 0.01f)
      {
          // Calculate the rotation to face the direction vector.
          Quaternion targetRotation = Quaternion.LookRotation(direction.normalized);
  
          // Apply the rotation to the missile's transform.
          transform.rotation = targetRotation;
      }
  
      // Update the previousPosition to the current position for the next frame.
      previousPosition = transform.position;
  }
}

Attach this script to your missile game objects. It will make the missiles automatically face the direction they are moving, without relying on the physics engine (Rigidbody). The Update() method will be called every frame to update the missile’s rotation based on its movement direction.

Note: This approach assumes that the missile’s movement is controlled by directly setting its transform.position in your game code. If you’re using other means to move the missile (e.g., adding forces using Rigidbody), you may need to adjust the approach accordingly.

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

public class MissileController : MonoBehaviour
{
    public GameObject playerReferance;
    public float speed;
    private GameObject missileReferance;
    private Vector3 playerPos;
    private Vector3 missilePos;
    private Vector3 previousPosition;
    // Start is called before the first frame update
    void Start()
    {
        playerPos = playerReferance.transform.position;
        missilePos = this.transform.position;
        previousPosition = transform.position;

    }

    // Update is called once per frame
    void Update()
    {
        attackPlayer(missilePos, playerPos);
        faceDirection(playerPos);

    }
    Vector3 attackPlayer(Vector3 startingPosition, Vector3 targetPosition)
    {
        Vector3 distanceToTarget = targetPosition - startingPosition;
        Vector3 direction = distanceToTarget.normalized;
        Vector3 velocity = direction * speed;
        Vector3 stepThisFrame = velocity * Time.deltaTime;
        this.transform.position += stepThisFrame;

        return stepThisFrame;
    }
    void faceDirection(Vector3 targetPosition)
    {
        Vector3 direction = transform.position - previousPosition;

        if (direction.sqrMagnitude > 0.01f)
        {
            Quaternion targetRotation = Quaternion.LookRotation(direction.normalized);
            transform.rotation = targetRotation;
        }

        previousPosition = transform.position;
    }
}

I’m not sure exactly what is going on, but if you are setting the position directly through script, you should be able to take the Vector between the object and it’s new position, and use that to set the rotation, for example:

Vector3 direction = targetPosition - transform.position;
transform.rotation = Quaternion.Euler(direction);// mod comment: this line is wrong

This should also work in 2D space if that is the case. Hope this helps out.