• 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
0
Question by funkmaster5000 · May 07, 2013 at 12:55 PM · multiplayer2d rotationbillboard

2D Billboard Rotation in Multiplayer?

Hi Community!

I'm working on a 2D/3D mix currently. My 2D objects consist of a quad, which is textured and rotated to always face the player. I'm using the CameraFacing.cs (original by Neil Carter, modified by Hayden Scott-Baron) script and it works so nice for SP. I use the settings Reverse Face: Yes, Axis: forward.

But when I want to go for MP, I have no idea how to handle this, because the actual sprite is rotated and obviously can't always face both players. So what can I do?

One thing I've tried is, that my 2D objects consist of two quads, one showing the front and one showing the back and being rotated by 180 degrees and made a child of the first quad.

This does work for one camera. If I turn the "Always face the camera" script off and walk behind my 2D character, it shows it's back. Wonderful! If I'm facing the 2D character from the side it is (which is no surprise) flat! So it needs to rotate with both cameras, but somewhat independent? How do I determine, that a player is in front or at the back of my sprite?

Another idea of me was, that I make a sprite sheet, showing both the front and the back. Then I determine the position of the player and either show the back of the sprite or the front (see professional image). This seems like a good idea, but I'm still facing the problem of the independent rotation. This also looks a lot like a problem to solve with atan/arctan (don't know the proper english term) at least in the BASIC progamming language (which is the one, I've started with)? I could make the enemy randomly pick one of the players to face and then calculate the corresponding angle to the position of the non-picked player. Maybe something like that...

Help is greatly appreciated! Thanks in advance.

alt text

playe.png (6.3 kB)
Comment
Add comment · Show 4
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 robertbu · May 07, 2013 at 02:54 PM 0
Share

I don't fully understand your problem. But in terms of front and back, you can just compare the forward vector of your sprite to the forward vector of your camera(s). See: Vector3.Dot() or Vector3.Angle(). Note you might have to synchronize the 'Y' values of the two vectors to get values like you would expect.

If you want to calculate it as a 2D problem using absolute angles, See $$anonymous$$athf.Atan2().

avatar image funkmaster5000 · May 10, 2013 at 11:59 PM 0
Share

I actually have a right example: The impression of my 2D characters given at 1:13 http://www.youtube.com/watch?v=7l$$anonymous$$0Xf$$anonymous$$Q4u8

avatar image Bunny83 · May 11, 2013 at 12:26 AM 2
Share

@funkmaster5000: Just for future youtube links, you can set a start time in the videolink by adding a "&t" parameter and your desired time. Like this:

http://www.youtube.com/watch?v=7l$$anonymous$$0Xf$$anonymous$$Q4u8&t=1m12s

avatar image funkmaster5000 · May 11, 2013 at 10:07 AM 0
Share

Thanks, nice to know!

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Loius · May 07, 2013 at 08:15 PM

Just face the panel to the active camera.

If the players are client-server, everyone has their own camera. If it's local play on one instance of game, you want the panel facing the camera anyway so it doesn't distort.

panel.transform.forward = Camera.current.transform.position - panel.transform.position;

Comment
Add comment · Show 3 · 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 funkmaster5000 · May 08, 2013 at 08:30 AM 0
Share

True. I've read a lot about $$anonymous$$P yesterday. So here's the plan:

  • The server holds the original position of the object

  • The rotation and "animation" happens local

  • If the enemy moves, the clients are told it's new position

  • If player Z > Enemy Z : Show back image (local)

  • If player Z <= Enemy Z : Show front image (local)

How can I compare these values?

avatar image Loius · May 09, 2013 at 09:57 PM 0
Share
 float angle = Vector3.Angle(Camera.current.transform.forward, enemy.transform.forward);
 // angle == 0 => camera-forward is exactly enemy-forward => camera looking at enemy's back
 // angle==180 => looking directly at enemy's face
 if ( angle < 90 ) ShowFront(); else ShowBack();

That runs purely from camera view, although if you are able to shorten it to just a z compare it's

 if ( enemy.transform.position.z > player.transform.position.z ) ShowBack(); else ShowFront(); // or whatever
avatar image funkmaster5000 · May 11, 2013 at 07:14 PM 0
Share

This is the code, I'm using now! When I try to use Camera.current I get a Null Exception Error. If I save it in a Transform variable everything is okay. Do you happen to know why?

 var behind : boolean = false;
 
 function Start () {
 
 }
 
 function Update () {
     transform.Translate(0,0,-(1 * Time.deltaTime), Space.World);
     
     if (transform.position.z <= Camera.current.transform.position.z)
     {
         if(behind == false)
         {
             renderer.material.mainTextureOffset.x += 0.5;
             behind = true;
         }
     }
         
     if (transform.position.z > Camera.current.transform.position.z)
     {
         if (behind == true)
         {
             renderer.material.mainTextureOffset.x -= 0.5;
             behind = false;
         }
     }
 
 }
avatar image
0

Answer by cowlinator · May 07, 2013 at 04:20 PM

One way to do this would be to create a parent object for the location of the 2D object, and a bunch of duplicates of the actual 2D object as the children. Put each child on a different layer, and put one layer in the culling mask for each multiplayer camera.

A more graceful solution would be to write or find a camera-facing shader, but if you don't know anything about shaders this could get complicated for you.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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 avatar image avatar image avatar image

Related Questions

Why is the Unity 2D physics engine only deterministic when Z axis rotation is frozen? 0 Answers

World Space UI facing multiple Cameras (Billboard) 0 Answers

Unity networking tutorial? 6 Answers

Can I disable UI visibility for the LOCAL player and not other players? (using photon) 0 Answers

Save data with acces for all player 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