Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 $$anonymous$$ · Jun 05, 2014 at 05:44 PM · positionmathcoordinateslocal axis

Get the position behind an object (locally) ?

Hey guys, I ran into a problem while making my complex(ish) camera behaviour. The problem that i have run to is that i am not able to get the position behind my player. Please don't tell me to use Vector3.back etc.. , because i want the position locally, to support turning. It won't work if the camera is always set to that position, cos i have a cool movement system in place.

I tried a number of approaches, and confused myself with the math. I also tried complex addition and subtraction. None of them worked at all.

I guess i am probably missing something quite simple, like a way to get into local coordinates, or etc..

Comment
Add comment · Show 1
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 Nick4 · Jun 05, 2014 at 05:49 PM 0
Share

Please be more specific. Is your game 2D or 3D? top-down or third person?

1 Reply

· Add your reply
  • Sort: 
avatar image
15

Answer by rutter · Jun 05, 2014 at 05:48 PM

Every GameObject has a Transform component, which can give you its local right, up, and forward vectors. In particular, multiplying local forward by -1 will give you a "backwards" direction.

The following would give you a point one unit behind your target:

 function GetBehindPosition(target : Transform) {
     return target.position - target.forward;
 }

Because the forward/right/up vectors are normalized (have a length equal to one), you can multiply them by any number X to get a point X units in that direction:

 function GetBehindPosition(target : Transform, distanceBehind : float) {
     return target.position - (target.forward * distanceBehind);
 }

You can even mix and match your offsets:

 function GetBehindPosition(target : Transform, distanceBehind : float, distanceAbove : float) {
     return target.position - (target.forward * distanceBehind) + (target.up * distanceAbove);
 }
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 Derptastic · Mar 27, 2017 at 10:05 AM 0
Share

$$anonymous$$ight I ask what would be the same solution in 2D space of your second piece of code (x units of distance behind given object)? Relative to the targeting object's position (the 'behind' should change, depending on the position of the targeting object, regardless of where the target is facing).

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

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

Related Questions

Make something arrive at position in exactly X seconds? 2 Answers

arc object's position using sin 1 Answer

Calcultate spawn position based on spawn volume's rotation 2 Answers

AngryBots mobile joysticks position 0 Answers

Find intersection (with Picture) 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