• 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 rudidev08 · Mar 25, 2012 at 08:55 PM · rotationangle

Draw a game object between two objects (for example, a beam), struggling with angle

Hola!

I'm instantiating a new GameObject from a prefab, which is most often going to be a attack laser/beam/etc. So, I want it to be drawn between two objects (which I call here origin and target).

I figure that beam object should be positioned in a center point between origin and target, and that part works well... and I can also calculate scale based on the distance between the two:

 // get the middle point between the objects
 Vector3 center = Vector3.Lerp(origin.transform.position, target.transform.position, 0.5F);

Part where I'm struggling with is rotating the beam object so it looks like it's connecting the origin and target. It's a 3d space, but angle is 2d since Y plane is fixed.

Here's the code I've had that failed miserably:

 // this angle is always too small, while it should go from 0 to 360 or such
 float angle = Vector2.Angle(new Vector2(origin.transform.position.x, origin.transform.position.z), new Vector2(target.transform.position.x, target.transform.position.z));

I've also tried snippets with Quaternion.FromToRotation, Vector3.Cross, .LookAt, and very much anything I could google with finding angle between two objects.

At this point I'm about to go back to basics and calculate angle myself between two points, just like we did in college, but I hope there's more elegant solution out there, especially with all built-in Unity functions.

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

1 Reply

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

Answer by rutter · Mar 25, 2012 at 11:13 PM

You may have an easier time using a LineRenderer or TrailRenderer.

If you really need a manual solution, I'd say this is the sort of problem that's much easier to solve with vectors directly. For example, you could call LookAt() on the beam object's transform, forcing it to "face" the target, and then scale the beam object along its forward axis proportional to the distance between shooter and target positions.

Comment
Add comment · Show 1 · 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 rudidev08 · Mar 25, 2012 at 11:43 PM 0
Share

Fantastic, thank you! I didn't look much into Effects, except for Particle System, and LineRenderer is exactly what I needed!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Add Force to the right of the rigidbody, not right of the screen 1 Answer

Problems caused by non-unique Euler angle solutions 1 Answer

Restricting rotation to multiples of 15? 1 Answer

How do I apply rotations to parent child using Rigidbody? 0 Answers

90 Degree stopping rotation on y-axis issue 0 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