How do I make a sprite face the ground while moving?

Hello, Im new to unity and am following some tutorials on how to make a 2D side scroller game. Im trying to branch out of the tutorials, but I dont know how to solve this problem. I want my wizard to shoot fireballs and Ive succeeded in make them fall due to gravity. However, the texture is not pointing down when its falling. Btw, I am using an empty object with a line renderer to spawn the objects and I put a rigid body on so it falls. I would greatly appreciate if somebody could help me out here.

Assuming the sprite is pointing right by default and it has a RigidBody2D attached…

void Update () {
        transform.right = GetComponent<Rigidbody2D>().velocity;
    }

You may want to cache a reference to the RigidBody2D rather than use GetComponent() in the Update() method.

This should do the trick.

[RequireComponent(typeof(Rigidbody2D))]
public class RotateToVelocity : MonoBehaviour
{
    private Rigidbody2D rBody2D;

    void Start()
    {
        rBody2D = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        Vector2 heading = rBody2D.velocity;
        if (heading.sqrMagnitude > 0.001f)
        {
            heading.Normalize();
            float angle = (Mathf.Atan2(heading.y, heading.x) * Mathf.Rad2Deg);
            transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
        }
    }
}

Where should

Where should I add this? Just as another script on the prefab or on the actual movement script of the prefab.

using UnityEngine;
using System.Collections;

public class MoveTrail : MonoBehaviour {
   
    public int moveSpeed = 230;
   
    // Update is called once per frame
    void Update () {
        transform.Translate (Vector3.right * Time.deltaTime * moveSpeed);
        Destroy (gameObject, 1);
   
    }
}

This is the script that they told me to attach to the prefab. There is also a line renderer and a texture that I attached to the empty object.

Maybe this will work… I haven’t tested it though.

using UnityEngine;
using System.Collections;
public class MoveTrail : MonoBehaviour {
 
    public int moveSpeed = 230;
    private Vector3 velocity;
  
    void Start()
    {
         Destroy (gameObject, 1);
    }
    // Update is called once per frame
    void Update () {
       
        velocity = Vector3.right * Time.deltaTime * moveSpeed;
       
        float angle = (Mathf.Atan2(velocity.y, velocity.x) * Mathf.Rad2Deg);
        transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));

        transform.Translate (velocity);
    }
}

Its weird, I have two scripts: one that is called “MoveTrail” and one that is called “Facedirection”. I copy pasted the script that you sent me on to “Move Trail” and it seems like nothing happens. So I deleted it. Then I copy pasted it into the “Facedirection” script (deleted everything that was there before in both cases) and the texture did face where my mouse was firing, but the projectiles did not move. Also, is it possible for the projectiles to face the direction of their speed? So that they face down as they fall.

Honestly if your Bullet prefab has a RigidBody2D on it you shouldn’t really be moving it using Transform.Translate. You should use RigidBody2D.AddForce method.

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Rigidbody2D))]
public class Bullet : MonoBehaviour
{
    public float Speed;
    public float TurnRate;

    private Rigidbody2D rBody2D;
    private Vector2 velocity;

    void Start()
    {
        rBody2D = GetComponent<Rigidbody2D>();
        velocity = Vector2.zero;
    }
    void Update()
    {
        velocity = (Vector2.right * Speed);

        Vector2 heading = velocity;
        if (heading.sqrMagnitude > 0.001f)
        {
            heading.Normalize();
            float targetAngle = (Mathf.Atan2(heading.y, heading.x) * Mathf.Rad2Deg);
            float rotationAngle = Mathf.LerpAngle(transform.rotation.eulerAngles.z, targetAngle, TurnRate * Time.deltaTime);
            transform.rotation = Quaternion.Euler(new Vector3(0, 0, rotationAngle));
        }
    }
    void FixedUpdate()
    {
        rBody2D.AddForce(velocity);
    }
}

Add this to your bullet/projectile prefab. This should make the bullet move to the right and turn towards the direction its moving. This is another script I have not tested btw…

Hmm, it seems that the sprite is facing the side that my mouse is in, but it isnt launched, it just falls directly downwards from the place its being spawned in.