Search Unity

8 Dir Billboard Update

Discussion in 'General Graphics' started by Nails-of-Scream, Sep 17, 2019.

  1. Nails-of-Scream

    Nails-of-Scream

    Joined:
    Jul 27, 2016
    Posts:
    21
    I have come back for help. How do I make a 8 directional sprite rotate?

    I'm trying to make my own system that is animated with Unity's animation tools and doesn't require SignedAngle. The script I have is this.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DirectionalSprite : MonoBehaviour
    6. {
    7.     public int directionInt;
    8.     public float charRot;
    9.  
    10.     public float playerAngle;
    11.     Vector2 cameraToSprite;
    12.     Transform playCamera;
    13.     Vector3 direction;
    14.     float angle;
    15.     public float monAngle;
    16.  
    17.     public DemonScript demon;
    18.  
    19.     void Awake()
    20.     {
    21.         playCamera = GameObject.FindWithTag("MainCamera").transform;
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         monAngle = transform.rotation.y * 1;
    28.         direction = playCamera.transform.position - transform.position;
    29.         playerAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
    30.         playerAngle += charRot;
    31.  
    32.         if (charRot > 180)
    33.         {
    34.             charRot = 0;
    35.         }
    36.  
    37.         if (charRot < 0)
    38.         {
    39.             charRot = 180;
    40.         }
    41.  
    42.  
    43.         if (playerAngle >= 0f && playerAngle < 45f)
    44.         {
    45.             directionInt = 0;
    46.         }
    47.         else if (playerAngle >= 45f && playerAngle < 90f)
    48.         {
    49.             directionInt = 1;
    50.         }
    51.         else if (playerAngle >= 90f && playerAngle < 135f)
    52.         {
    53.             directionInt = 2;
    54.         }
    55.         else if (playerAngle >= 135f && playerAngle < 180f)
    56.         {
    57.             directionInt = 3;
    58.         }
    59.         else if (playerAngle >= -180f && playerAngle < -135f)
    60.         {
    61.             directionInt = 4;
    62.         }
    63.         else if (playerAngle >= -135f && playerAngle < -90f)
    64.         {
    65.             directionInt = 5;
    66.         }
    67.         else if (playerAngle >= -90f && playerAngle < -45f)
    68.         {
    69.             directionInt = 6;
    70.         }
    71.         else if (playerAngle >= -45f && playerAngle < 0f)
    72.         {
    73.             directionInt = 7;
    74.         }
    75.  
    76.         demon.SetDirection(directionInt);
    77.     }
    78. }
    Based of my first attempt at 8 dir sprites. But the problem I'm encountering is how to rotate the character. Any help to give?