• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

Directional Sprites

Status
Not open for further replies.

Screamernail

S

Screamernail

So I've found this script on the Unity forums when I googled for tutorials.

Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpriteDirection : MonoBehaviour {

    Transform player;
    public float angle;
    Vector3 direction;

    public Renderer spriteObj;

    public Material[] mat;

    void Awake () {
        player = GameObject.FindWithTag("Player").transform;
    }
   
    void Update () {
        direction = player.transform.position - transform.position;
        angle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
        ChangeDirection();
    }

    void ChangeDirection()
    {
        if (angle < 0) angle += 360;
        spriteObj.sharedMaterial = mat[(int)Mathf.Round(angle / 360f * mat.Length) % mat.Length];

    }
}

I've tested it and it worked! But I do wonder if I should still use this for my shooter game. Not much to say just showing that I'm still alive.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,217
Technically there are optimizations available if one wants to rigidly lock angle such as in old school shooters like Contra, or if one knows how many directional sprites are available to start with (constant number). However there is nothing to gain from it on modern computers even when running several hundred such sprites.
 

Screamernail

S

Screamernail

I didn't intent to have locked angles. I just don't know what to do.
 
Status
Not open for further replies.
Top