Screamernail
S
Screamernail
So I've found this script on the Unity forums when I googled for tutorials.
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.
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.