• 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 JoeGtake2 · Jun 13, 2014 at 06:54 PM · positionrelative

Positioning an object in relation to others

I think this is my trig failing me here - but I wonder if there is a simper way to do this.

I have object #1. It is moving and rotating based on player control.

I have object #2. Call this a target. Object #1 and the target have no direct relationship.

I have object #3. Call this looker. I need looker to always stay on the far side of Object #1 in relation to Target. If you can imagine an imaginary line running from Target, it should run through Object #1 and then Looker should always be just a little further on that line.

I know how to get the angle, but don't know how to position object 3 in the right place.

Any thoughts? Thanks!

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 robertbu · Jun 13, 2014 at 06:58 PM

Assuming object1, object2, and looker are transforms of the three objects:

 var v = object1.postion - object2.position;
 looker.position = object1.position + v.normalized * someDist;

'someDist' is the distance beyond object1 for looker. Assuming you want looker to look at the two objects:

 looker.rotation = Quaternion.LookRotation(-v);
Comment
Add comment · Show 4 · 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 JoeGtake2 · Jun 13, 2014 at 07:19 PM 0
Share

Thanks! This really helped! Just one more slight modification - if I wanted to offset the degrees of the angle slightly, how could I do that simply? Right now, it places Looker object in that direct line (what I had requested). If I wanted to offset the position (illustratively, looking at 12:15 rather than 12:20, for example), is there a simple way to do this?

Thanks again.

avatar image robertbu · Jun 13, 2014 at 07:34 PM 0
Share

Rather than angle, it would be easiest to offset it by distance. Assu$$anonymous$$g that Vector3.up is the up for your objects, and 'offsetDist' is the amount you want to offset, you can do:

 var v = (object1.postion - object2.position).normalized;
 var v2 = Vector3.cross(Vector3.up, v);
 looker.position = object1.position + v * someDist + v2 * offsetDist;
avatar image JoeGtake2 · Jun 13, 2014 at 07:43 PM 0
Share

I could be mistaken, but I believe what you meant to write was:

 looker.position = (obj1.position + v * dist) + (v2 * lookerOffset);


That seems to be working (the multiplication of v2 and offset rather than addition?)

Thanks again!

avatar image robertbu · Jun 14, 2014 at 12:01 AM 0
Share

Yep, should have been a multiplication. I fixed the original comment.

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

2 People are following this question.

avatar image avatar image

Related Questions

need rotate when change position relative to char 1 Answer

Comparing to a relative position. 1 Answer

Camera rotation around player while following. 6 Answers

How do you move the camera relative to the direction it is currently facing? 1 Answer

How to give a relative position to a GUI Texture ? 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