Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by $$anonymous$$ · Feb 23, 2013 at 05:45 PM · c#directionangle

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Feb 27, 2013 at 12:17 PM 0
Share

I hate to bump,but this question went way under the new ones too fast.

avatar image Tarlius · Feb 28, 2013 at 07:00 AM 0
Share

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?

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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?

Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Feb 28, 2013 at 10:54 AM 0
Share

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.

avatar image Tarlius · Feb 28, 2013 at 11:06 AM 1
Share

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 :)

avatar image $$anonymous$$ · Feb 28, 2013 at 11:12 AM 0
Share

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.

avatar image Tarlius · Mar 01, 2013 at 01:14 AM 1
Share

I think its the z component of the forward that you want.

avatar image $$anonymous$$ · Mar 02, 2013 at 10:26 PM 0
Share

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];

Show more comments
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges