- Home /
8 Directional sprite in 3d world, how to retrieve direction?
This is a part#2 question of this one: http://answers.unity3d.com/questions/401695/8-directional-sprite-in-3d-world-how-to-retrieve-a.html
Now I'm trying to retrieve also the direction of the object as Mathf.Atan2 it only give the angle between two objects ut not the direction, so it doesn't matter if the object is rotated or not. But considering if you read the previous question (and the posted solution) I need also to know where the object with the sprite is heading, as it will be moving in the 3d space, so if the sprite is heading north for example the player should see the back of the character.
I'll repost the previous question script just to have something to look at, how can I retrieve the direction of the sprite and put that in the math function I already have?
(UPDATED):
using UnityEngine; using System.Collections;
[AddComponentMenu("Sprite Scripts/Directional Sprite")] public class DirectionalSprite : MonoBehaviour {
public int colCount = 4;
public int rowCount = 4;
public int rowNumber = 0; //Zero Indexed
public int colNumber = 0; //Zero Indexed
public int totalCells = 4;
public int fps = 10;
int[] animIndex = {0,1,2,3,4,5,6,7};
int index;
int uIndex;
int vIndex;
public Vector2 size;
Vector2 offset;
float offsetX;
float offsetY;
Vector3 playerPos;
Vector3 spritePos;
Vector2 spriteFacing;
Vector2 playerToSprite;
Transform player;
Vector3 direction;
float angle;
public Renderer spriteObj;
void Awake () {player = GameObject.FindWithTag("Player").transform;}
void Update () {
playerPos = player.transform.position;
spritePos = transform.position;
spriteFacing = new Vector2(transform.forward.x, transform.forward.y);
playerToSprite = new Vector2(spritePos.x, spritePos.z) - new Vector2(playerPos.x, playerPos.z);
direction = playerToSprite - spriteFacing;
angle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
//Set orientation and snimation set
SetSpriteOrientation();
SetSpriteAnimation(rowNumber,colNumber,totalCells,fps);
}
//Set sprite orientation
void SetSpriteOrientation () {
if (angle < 0) angle += 360;
rowNumber = animIndex[(int)Mathf.Round(angle / 360f * colCount) % rowCount];
}
//SetSpriteAnimation
void SetSpriteAnimation(int rowNumber ,int colNumber,int totalCells,int fps ){
// Calculate index
index = (int)(Time.time * fps);
// Repeat when exhausting all cells
index = index % totalCells;
// split into horizontal and vertical index
uIndex = index % colCount;
vIndex = index / colCount;
// build offset
// v coordinate is the bottom of the image in opengl so we need to invert.
offsetX = (uIndex+colNumber) * size.x;
offsetY = (1.0f - size.y) - (vIndex + rowNumber) * size.y;
offset = new Vector2(offsetX,offsetY);
spriteObj.renderer.sharedMaterial.SetTextureOffset ("_MainTex", offset);
}
}
Webplayer initial test:
http://games.neuro-lab.net/resources/Build/Build.html
Use WASD and mouse to move around.
Cyberdemon and hexen textures used as test, no copyright infringment intended.
I hate to bump,but this question went way under the new ones too fast.
You want to a way to select the correct image to use for a sprite that can be looked at from any angle in 3D?
Have I understood the question correctly?
I assume you already have a billboarding script to make the sprite face the camera at all times?
Answer by Tarlius · Feb 28, 2013 at 07:20 AM
Vector3 cameraPos = ; // fixme
Vector3 spritePos = ; // fixme
Vector2 spriteFacing = new Vector2(sprite.transform.forward.x, sprite.transform.forward.y);
cameraToSprite = new Vector2(spritePos.x, spritePos.z) - new Vector2(cameraPos.x, cameraPos.z);
I was half way through looking up the rest, but it looks like you already have the "hard" bit done :) That code will get you a vector from the camera to the sprite, and a vector for the direction the sprite is looking, in 2D.
I was thinking about a similar problem a few days ago. What will you do when the camera goes over the top of a sprite? Do you have sprites for a top-down view as well? Or will the camera never be high-up enough for it to matter?
Is not problem if you see the sprite from above, I use billboarding so I'm interested only in x and z, updated my question with a webplayer example to show you. I'm going to try this suggestion and work out some math on the angle calculation.
I just checked the demo out looks pretty good. Seems to be looking pretty good, but I assume the problem is it only works if the enemy is facing that way?
Either way, is a cool effect. Very old school :)
Forgot to mention you can move around in the demo using WASD and mouse. And yeah the problem is that you can look around the sprite and see it changing, but if the sprite object rotate or moves won't update the sprite as my operation only work depending on player and sprite position but not direction. So you can see the enemy moonwalk if it moves and you see from a certain angle.
By the way how can I apply your vectors calculation to my operation? Sorry I'm just bad with that kind of math.
Also updated question with my most recent code that now have also sprite animation (using the texture animation from the wiki) and trying to get your solution to work.
I think its the z component of the forward that you want.
I'm still trying to figure this out, no idea how to proceed and apply your vector calculation to the angle.
Here: angle = $$anonymous$$athf.Atan2(direction.x, direction.z) * $$anonymous$$athf.Rad2Deg;
and here: rowNumber = animIndex[(int)$$anonymous$$athf.Round(angle / 360f * colCount) % rowCount];
Answer by TapSkill · Mar 03, 2013 at 02:48 AM
I kept things simple with my project and used a fixed camera angle, but yes, it seems you have the hard work done. You just need to have a management method to handle what angle the camera is looking, what direction the sprite is looking, and a billboard sprite script to keep it looking at the camera. Then, you need to have some logic like: if (sprite.direction == left && camera.direction == right) { sprite.appearance = up; } Basically, when the sprite is facing left (animation-wise) but the camera is to the right of the sprite (or looking left), the sprite should play its up direction animation.
Your answer
Follow this Question
Related Questions
how to get a correct rotation 2 Answers
How to calculate direction between 2 objects 2 Answers
Weird angle returned 1 Answer
How to get the direction of the velocity of an object (c#) 1 Answer
Multiple Cars not working 1 Answer