1. The results for Texturing Contest #28 are out! Step by to congratulate our winners!
    Dismiss Notice
  2. Join us in our custom games night on Saturday, July 14. If you'd like to create a map for the night, check out the map challenge!
    Dismiss Notice
  3. Join in on Icon Contest #17 and show your interpretation of a legendary equipment set!
    Dismiss Notice
  4. We've created the Staff Job Openings thread. We're currently in need of icon, video production, and social/multimedia positions to be filled. Thank you!
    Dismiss Notice
  5. Music Contest #10 Retro is out! Join us for some retro/vintage fun!
    Dismiss Notice
  6. Don't be stagnant - embrace change! The time has come to evolve and join the Techtree Contest #12 - Evolution.
    Dismiss Notice
  7. On May 20th a new law about privacy and data processing comes into work in the EU. I am no lawyer and I need help figuring out if we comply and if not, what we must do about it. Please message me if you can provide any assistance. Read more. Ralle
    Dismiss Notice

Directional Sprites

Discussion in 'Unity 3D' started by Screamernail, Jul 13, 2018 at 7:41 PM.

  1. Screamernail

    Screamernail

    Joined:
    Aug 12, 2012
    Messages:
    233
    Resources:
    0
    Resources:
    0
    So I've found this script on the Unity forums when I googled for tutorials.

    Code (Text):
    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.
     
  2. Dr Super Good

    Dr Super Good

    Spell Reviewer

    Joined:
    Jan 18, 2005
    Messages:
    24,460
    Resources:
    3
    Maps:
    1
    Spells:
    2
    Resources:
    3
    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.
     
  3. Screamernail

    Screamernail

    Joined:
    Aug 12, 2012
    Messages:
    233
    Resources:
    0
    Resources:
    0
    I didn't intent to have locked angles. I just don't know what to do.