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 Guirao · Sep 02, 2013 at 07:34 PM · meshruntimeflashlightbillboardquad

Create facing camera quad at runtime given start and end points

Hello, I want to create a quad at runtime given 2 points (the middle center edges) trying to face the camera at all time.

What I'm trying to achieve is a the flashlight effect, I want to throw a raycast from the flashlight (point A) to whatever object it touches (point B) and then create quad "trying to face" the camera.

Here's the quad I want to achieve.

alt text

A is the starting point of the quad and the middle point of vertices X and Y, and B is the ending point and middle point of vertices Z and V. I say trying because obviously the quad won't be able to face the camera like a billboard 100% because of the fixed points A and B, I don't know what calculations I have to do to get points X, Y, Z and V.

unity.jpg (70.9 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 · Sep 04, 2013 at 05:33 AM 0
Share

You are asking specifically for the coordinates of X,Y,Z,V. Is this really what you need, or are you just trying to put a quad in the position you specify in your question?

avatar image Guirao · Sep 04, 2013 at 07:08 AM 0
Share

I need coordinates X,Y,Z and V given A and B which are the middle points of XY and ZB respectively. Put it this way, given 2 points A and B I need a plane which is oriented to the camera but the orientation can only be in the AB axis.

avatar image robertbu · Sep 04, 2013 at 07:39 AM 0
Share

I understand what you are asking for. What I mean is that it would be easier to start with an already existing Quad either from the Game Object menu if you have a recent version or using the CreatePlane editor script, and then position and scale the quad to match the parameters rather than to calculate these specific points. Anyway I don't have time at the moment to write you an answer, but I'll answer this evening (morning now).

avatar image Guirao · Sep 04, 2013 at 08:51 AM 0
Share

Is it possible to scale the quad to match the distance of AB segment?

1 Reply

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

Answer by robertbu · Sep 04, 2013 at 04:37 PM

I'm going to give you what I think you want rather than what you specificallly asked for. If it is not right, let me know, and I can revise. Normally with a rotation problem I've not solved before, I test it in Unity, but I'm traveling and don't have access to a computer with Unity at the moment. So to make this work you need to start with a vertical plane one unit on a side with the normals facing backwards. The latest versions of Unity have a Quad on the Game Object menu that will work, or you can use a plane created using the Unity Wiki CreatePlane script with a 'vertical' specification. 'v3A', and 'v3B' are two Vector3s containing your positions A and B from your diagram above:

 var direction = v3B - v3A;
 var midPoint = v3A + direction * 0.5;
 var fromCamera = transform.position - Camera.main.transform.position;
 transform.position = midPoint;
 transform.localScale = Vector3(direction.magnitude, height, 1);
 transform.forward = fromCamera;
 transform.rotation = Quaternion.FromToRotation(transform.right, direction) * transform.rotation;

Note you did not specify the height of your Quad, so I just used 'height'. The code places a Quad at the midpoint between A and B, then it scale it so that it will span from A to B along the local 'x' axis, and finally it first rotates it so that it is billboarded to the camera and then does an additional rotation to align the local X (left and right of the plane) with the A to B direction.

Comment
Add comment · Show 5 · 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 Guirao · Sep 07, 2013 at 01:16 PM 0
Share

Hmm, but this is rotating the object, what i meant was calculating the world coordinates of the vertices.

avatar image robertbu · Sep 08, 2013 at 04:43 AM 0
Share

First, if you apply the above code to an empty game object, you can use the corners to calculate the four world coordinates. That is you can use Transform.TransformPoint() to the local coordinates of: (-0.5, 0.5, 0), (0.5, 0.5, 0), (0.5, -0.5, 0), and (-0.5, -0.5, 0). But you can also calculate them more directly:

 var v3Right = (v3B - v3A).normalized;
 var fromCamera = transform.position - Camera.main.transform.position;
 var halfWidth fromCamera.magnitude / 2.0; var v3Up = Vector3.cross(v3Right,fromCamera).normalized.
         
 var upperLeftCorner = -v3Right * halfWidth + v3Up * halfHeight;
 var upperRightCorner = v3Right * halfWidth + v3Up * halfHeight;
 var lowerLeftCorner = -v3Right * halfWidth - v3Up * halfHeight;
 var lowerRightCorner = v3Right * halfWidth - v3Up * halfHeight;

I don't have access to Unity at the moment, so this is untested. You may have to fiddle a bit. In particular, I may have the parameters to the Vector3.Cross() backwards.

avatar image Guirao · Sep 08, 2013 at 10:44 AM 0
Share

I've tried this but the quad starts way back point A but it finishes correct at B.

avatar image robertbu · Sep 09, 2013 at 05:26 AM 0
Share

What do you mean by 'this'? Please post your code so I can take a look.

avatar image Guirao · Sep 09, 2013 at 07:08 AM 0
Share

Your last code, but yeah now I'm getting the idea of the mathematics involved here.

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

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

Related Questions

Save to Prefab or Mesh? 0 Answers

Issue mapping texture to imported quad 1 Answer

Dynamic conversion of any uploaded mesh to assetbundle? 0 Answers

Vertex snapping of any mesh at runtime 2 Answers

change the mesh of object during runtime using javascript 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