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
0
Question by Remy · Nov 24, 2010 at 06:49 PM · camerainstancefacingalign

Instanced Prefabs won't face camera.

Hi,

I created a scene where a script attached to an empty GameObject, instances prefab's into the scene. See images. (test scene, objects will be replaced)

setup

3d grid

So far so good!. I want the objects in the scene to face the camera. These cubes will be replaced by - camera facing -billboards.

Here's the script that's attached to the empty GameObject:

var prefab : GameObject;

var gridX = 5; var gridY = 5; var gridZ = 5; var spacing = 2.0;

function Start () { for (var z = 0; z < gridZ; z++) { for (var y = 0; y < gridY; y++) { for (var x= 0; x < gridX; x++) { var pos = Vector3 (x, z, y) * spacing; Instantiate(prefab, pos, Quaternion.identity); } } } }

In the inspector I drag a prefab (cube) to it. So far so good. It creates the 3d cube grid.

I've tried several ways to have the instanced cubes oriented to the camera. The scripts work on non instanced - instanciate() - objects. Drag the prefad to the hierarchy, attach script and the object faces the cam.

The look at cam script:

var cameraToLookAt: Camera; 

function Update() { var v: Vector3 = cameraToLookAt.transform.position - transform.position; v.x = v.z = 0.0; transform.LookAt(cameraToLookAt.transform.position - v); }

Q: How do I get the instanced cubes (or other prefabs) face the camera?

Thank you

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Anton Petrov · Nov 24, 2010 at 08:28 PM

Usualy Update() does not work in edit time. And you should apply [ExecuteInEditMode] attribute to the Update method.

And your script looks bad. What is v.x = v.z = 0????

I can suggest you my billboard script:

public class Billboard : MonoBehaviour { public bool UniformSize = false; public float UniformSizeFactor = 0.1f;

 void Update () 
 {
     transform.LookAt( Camera.current.transform, Camera.current.transform.up );

     if ( UniformSize )
     {
         float scale = UniformSizeFactor * Vector3.Distance( Camera.current.transform.position, transform.position );
         transform.localScale = new Vector3( scale, scale, scale );
     }
 }

}

Here UniformSize and UniformSizeFactor will allow you to create billboard which do not change their size when camera moves.

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 Remy 1 · Nov 25, 2010 at 03:40 PM 0
Share

I added this line: using UnityEngine; or it wouldn't compile. After adding the script to the empty GameObject I get this error: NullReferenceException Billboard.Update () (at Assets/Billboard.cs:9) Something named wrong, or JS and C# can't work on the same object?

avatar image Remy 1 · Nov 25, 2010 at 03:41 PM 0
Share

To add: the error appear when I run the scene, than it pauses and give the error.

avatar image Anton Petrov · Dec 01, 2010 at 08:20 AM 0
Share

Looks like Camera.current is null? Please make some checks.

avatar image
0

Answer by Remy 1 · Nov 25, 2010 at 09:16 AM

Thank you Anton.

The scripts ain't mine. I'm not a coder. I will try to make the adjustments tonight.

I will let you know if it worked.

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

No one has followed this question yet.

Related Questions

Camera Auto-Align with the nearest object with a determinated tag 0 Answers

Camera Auto-Align with the nearest object with a determinated tag 0 Answers

camera script problems 2 Answers

Toggle Control between multiple turrets 0 Answers

RPG scripts 2 Answers


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