Author Topic: panels/sprites outside of NGUI camera and LookAt()  (Read 7630 times)

Zophiel

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 54
    • View Profile
panels/sprites outside of NGUI camera and LookAt()
« on: November 01, 2012, 08:01:19 AM »
I have an Effect Panel attached to a ship with a health sprite attached to it that i have looking at the camera.
But as the ship moves, the sprite get's distorted, which i think is due to the lookAt() code.

Any way to fix this?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: panels/sprites outside of NGUI camera and LookAt()
« Reply #1 on: November 01, 2012, 09:40:04 AM »
I don't know. Look at your code? o_O

That's all I can suggest as you didn't post any.

Btw, not sure why you would use LookAt here.

Zophiel

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 54
    • View Profile
Re: panels/sprites outside of NGUI camera and LookAt()
« Reply #2 on: November 01, 2012, 09:54:05 AM »
The only code involved is LookAt(); I'm trying to use NGUI as a replacement for some of my particle effects.

ShipGO
      -> UIPanel
               -> UISprite (with a tween along the y-axis)

It shows up, animates along the Y fine. Added a fade to fade out as well. But doesn't look at the camera. Adding a script that does this, either from the examples directory or of my own making, will distort the sprite.

PhilipC

  • Guest
Re: panels/sprites outside of NGUI camera and LookAt()
« Reply #3 on: November 01, 2012, 10:07:00 AM »
Take a look for the HUDText package. I think it will have example of what you want to do. (sounds like you want a 2d text to follow a 3D object on a different camera).

Zophiel

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 54
    • View Profile
Re: panels/sprites outside of NGUI camera and LookAt()
« Reply #4 on: November 01, 2012, 10:10:24 AM »
Not exactly, though i got that working already, as ArenMook said in another thread 'Widgets are just 3D objects'. So i'm keen to have an animating sprite be behind say the ship.
I've rendered some gun smoke to a sprite sheet and got it animating. But i'd like it to play where the cannon is actually fired while looking at the camera instead of overlaying the ship. Hence the hierarchy in the previous post.

In the above post, the lookat() code was distorting the sprite due to weird rotation from what i gather.
« Last Edit: November 01, 2012, 10:13:20 AM by Zophiel »

Zophiel

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 54
    • View Profile
Re: panels/sprites outside of NGUI camera and LookAt()
« Reply #5 on: November 02, 2012, 05:10:07 AM »
Do i need to have the panel face the camera or should i be able to do this on sprites?
Also what's the best way of tackling this in terms of code, do i need to use localrotation or something?

PhilipC

  • Guest
Re: panels/sprites outside of NGUI camera and LookAt()
« Reply #6 on: November 02, 2012, 05:49:34 AM »
Is your UI not being drawn by a different camera? because otherwise the UI will always be facing the camera with a rotation of 0,0,0

Zophiel

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 54
    • View Profile
Re: panels/sprites outside of NGUI camera and LookAt()
« Reply #7 on: November 04, 2012, 09:17:03 AM »
the UI yes, but not the sprite. The hierarchy is as follows:

Ship -> UIPanel -> UISprite
Main Camera
UI Root (2d) -> Rest of UI.



« Last Edit: November 04, 2012, 09:21:46 AM by Zophiel »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: panels/sprites outside of NGUI camera and LookAt()
« Reply #8 on: November 04, 2012, 01:47:47 PM »
That just seems wrong to me. You have a panel parented to an object that can move and rotate... and you want it to face the camera. Obvious question is -- what's the point of parenting it to that object to begin with? Again tho, if you want proper help, try posting some code. No one can guess what's wrong.

Zophiel

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 54
    • View Profile
Re: panels/sprites outside of NGUI camera and LookAt()
« Reply #9 on: November 05, 2012, 07:00:21 AM »
And as I mentioned the only code i have is 1 line that does a LookAt( Camera.main.transform ); on the panel. I just want to know if, and how, i can use a UISprite in the '3d world' and make it orientate towards the camera. They need to be able to be behind other objects instead of being part of the UILayer. I'd like to parent them to a ship because the ship is moving around while getting healed, fire shots, things like that.

There is no code to look at.

[Edit] And the reason for this setup was because you posted the following in this thread http://www.tasharen.com/forum/index.php?topic=1893.0
« Last Edit: November 05, 2012, 07:10:34 AM by Zophiel »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: panels/sprites outside of NGUI camera and LookAt()
« Reply #10 on: November 05, 2012, 11:35:05 AM »
Place this on your object and set the "target" to be your camera.
  1. using UnityEngine;
  2.  
  3. [AddComponentMenu("Game/Face Target")]
  4. public class FaceTarget : MonoBehaviour
  5. {
  6.         public Transform target;
  7.         public float speed = 1000f;
  8.  
  9.         Transform mTrans;
  10.  
  11.         void Start ()
  12.         {
  13.                 mTrans = transform;
  14.         }
  15.  
  16.         void LateUpdate ()
  17.         {
  18.                 if (target != null)
  19.                 {
  20.                         Vector3 dir = target.position - mTrans.position;
  21.                         float mag = dir.magnitude;
  22.  
  23.                         if (mag > 0.001f)
  24.                         {
  25.                                 dir *= 1f / mag;
  26.                                 Quaternion rot = Quaternion.LookRotation(dir);
  27.                                 mTrans.rotation = (speed > 0f) ? Quaternion.Slerp(mTrans.rotation, rot, Time.deltaTime * speed) : rot;
  28.                         }
  29.                 }
  30.         }
  31. }

Zophiel

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 54
    • View Profile
Re: panels/sprites outside of NGUI camera and LookAt()
« Reply #11 on: November 20, 2012, 12:02:16 PM »
Hello,

THanks for the script. It pointed me in the right direction. I have a question now in regards to 3D sprites. Do these need to be part of a 3D UI Root to be batched? I'm spawning a range of sprites with instantiate and in an empty scene, no matter how much, they will all batch and i'll have 1 draw call.

But in my game scene with a 2D UI it doesn't seem it all is part of 1 drawcall. It'll fluctuate between 2 - 4 draw calls.

All i'd like to know is if there is a certain way that i need to set it all up. Can i just spawn panels with sprites attached? And is there a point where the calls will be separated by unity?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: panels/sprites outside of NGUI camera and LookAt()
« Reply #12 on: November 20, 2012, 03:32:24 PM »
It all depends on their parent panel. Open the Panel Tool and select one of your sprites or a game object in the UI's hierarchy. The panel responsible for batching that widget will be highlighted.