Question by
Nails-of-Scream · Oct 11, 2019 at 08:44 AM ·
cameraspritevector3
8 Dir Sprites (nearer)
These scripts where from some time ago and now I've decided to get help with them. I've made a script for the sprite direction and one for the character.
1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DirectionalSprite : MonoBehaviour
{
public int directionInt;
public float charRot;
public float playerAngle;
Vector2 cameraToSprite;
Transform playCamera;
Vector3 direction;
float angle;
public float monAngle;
public DemonScript demon;
void Awake()
{
playCamera = GameObject.FindWithTag("MainCamera").transform;
}
// Update is called once per frame
void Update()
{
monAngle = transform.rotation.y * 1;
direction = (playCamera.transform.position - transform.position).normalized;
playerAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
playerAngle += charRot;
if (charRot > 180)
{
charRot = 0;
}
if (charRot < 0)
{
charRot = 180;
}
if (playerAngle >= 0f && playerAngle < 45f)
{
directionInt = 0;
}
else if (playerAngle >= 45f && playerAngle < 90f)
{
directionInt = 1;
}
else if (playerAngle >= 90f && playerAngle < 135f)
{
directionInt = 2;
}
else if (playerAngle >= 135f && playerAngle < 180f)
{
directionInt = 3;
}
else if (playerAngle >= -180f && playerAngle < -135f)
{
directionInt = 4;
}
else if (playerAngle >= -135f && playerAngle < -90f)
{
directionInt = 5;
}
else if (playerAngle >= -90f && playerAngle < -45f)
{
directionInt = 6;
}
else if (playerAngle >= -45f && playerAngle < 0f)
{
directionInt = 7;
}
demon.SetDirection(directionInt);
}
}
2
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DemonScript : MonoBehaviour
{
// Render
public SpriteRenderer rend;
public Sprite[] idleSprites;
public int spriteIndex;
int indexCount;
void Awake()
{
}
public void SetDirection(int directionName) {
switch(directionName){
case 0:
indexCount = 0;
rend.flipX = false;
break;
case 1:
indexCount = 1;
rend.flipX = false;
break;
case 2:
indexCount = 2;
rend.flipX = false;
break;
case 3:
indexCount = 3;
rend.flipX = false;
break;
case 4:
indexCount = 4;
rend.flipX = false;
break;
case 5:
indexCount = 3;
rend.flipX = true;
break;
case 6:
indexCount = 2;
rend.flipX = true;
break;
case 7:
indexCount = 1;
rend.flipX = true;
break;
}
}
void Update()
{
spriteIndex =+ indexCount;
rend.sprite = idleSprites[spriteIndex];
}
}
Any idea how I should do now? And what to fix?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612213259im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I get the world coordinates of the top left position of the camera? 3 Answers
Getting all sprites to render inside of the Perspective camera 1 Answer
Maintaining look at target between two cameras 1 Answer
ScreenCapture.CaptureScreenshotAsTexture() is making a milky white tinted screenshot. 1 Answer
cut out face from sprite using camera 0 Answers