Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 oltranzista · Sep 05, 2015 at 12:14 PM · vector3scriptingbasics

Extend a vector3 calculated from the difference between two vectors?

I'm trying to create a third person camera lock on system. If the player decides to lock on to an enemy, I want the camera to automatically center on the enemy and no longer accept input from the player. I got a basic script working to get the point in 3D space for the camera's position based on the direction between the player and enemy but I'm not sure if this is the best way to calculate this.

Here is the code I have so far. I'm afraid I may be missing some basic math that could be doing this simpler without having to resort to using the ".normalized".

 var direction = (player.position - enemy.position).normalized;
 var cameraDestination = player.position + (direction * offset);

Below is an image of what I'm trying to get (I'm trying to calculate where the blue dot should be in relation to the player and enemy).

alt text

example.png (19.2 kB)
Comment
Add comment · Show 3
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 Scribe · Sep 05, 2015 at 12:19 PM 1
Share

If you want a set 'offset' as you have currently, you need the normalize, looks good to me!

avatar image Bunny83 · Sep 05, 2015 at 01:08 PM 1
Share

@Scribe:

Just what i was about to add ^^.
If a fix distance to the player is desired there's no way around normalizing the direction since the (non-normalized) direction depends on the distance between the player and the enemy.

avatar image oltranzista · Sep 05, 2015 at 02:11 PM 1
Share

@Scribe, yes I did want a constant offset. Thanks for confir$$anonymous$$g this for me. I'm such a noob. :D

2 Replies

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

Answer by YoungDeveloper · Sep 05, 2015 at 12:16 PM

Why do think this approach is not simple?

You code does not have any problems, everything should be working fine.

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 oltranzista · Sep 05, 2015 at 02:08 PM 0
Share

Although I'm not new to program$$anonymous$$g, I am very new to Unity3D and game related math. I was just wanting to make sure I wasn't missing a better way to do this sort of thing as it seems very fundamental.

avatar image
2

Answer by wibble82 · Sep 05, 2015 at 12:39 PM

Looks perfectly valid to me. Only simplifcations you could theoretically make would be:

  • if you want the camera to be the same distance from the player as the player was from the enemy, then you just need to add the 'direction' without normalizing it

  • if offset is 0 then you obviously don't need to do anything :)

I'm guessing neither of those is true though, so you've got it exactly right. Nothing wrong with a normalize - they're on of the more expensive mathematical operations, but still very cheap compared to all the other stuff that's going on every frame.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

what is Vector3.normalized? 1 Answer

Application.LoadLevel problem. 1 Answer

Can you trick a Ui Button that it is being clicked with code? 3 Answers

difference between Gameobject.findobjectoftype<...>(); and simple findobjectoftype<...>(); ? 2 Answers

Axis disabled? (IO have not changed default input except fire1 and fire2) ? 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