• 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
Question by perezbalen · Oct 05, 2013 at 03:45 AM · offsetlocalglobalvectors

Add a Vector3 taking angle into account

Hi

I´m trying to move a GameObject a few units in a particular direction. To simplify, say I want a shield 1 unit in front of where the player is facing. I´m doing something like this:

shield.transform.position = player.transform.position + new Vector3(1f,0f,0f);

The problem I have is that the shield is always offset one unit in the x axes, regardless of the rotation of the player (when what I want is for it to be one unit in front of the player).

My guess is that my code adds the vector in global space, but I´ve been unable to make it add the vector in local space.

Thanks for any help.

EDIT: To clarify, the example is not the actual problem. So while doing it another way (like parenting) might fix this particular example, it´s not what I´m looking for (posting the actual problem might be a bit overwhelming, but in short there are a lot of calculations every frame that feed the x,y and z of the Vector3).

I need help in understanding why the adding of the vector is in global space, and how to do it in local space.

Thanks again.

Comment

People who like this

0 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 TrickyHandz · Oct 05, 2013 at 03:50 AM 0
Share

Have you considered making the shield a child of the player transform?

avatar image robertbu · Oct 05, 2013 at 04:11 AM 1
Share

@TrickyHandz's idea of making it a child is a great solution, but if you want to position it:

 shield.transform.position = player.transform.position + player.transform.forward * dist;

'dist' is the distance in front of the planer. If it is 1 unit like in your example, then you can leave it off.

avatar image weenchehawk · Oct 05, 2013 at 07:09 AM 0
Share

@perezbalen

The problem I have is that the shield is always offset one unit in the x axes, regardless of the rotation of the player (when what I want is for it to be one unit in front of the player).

The reason for this is you are placing it one unit in the x-direction

 shield.transform.position = player.transform.position + new Vector3(1f,0f,0f);

A Vector takes values Vector3(X, Y, Z), and you're adding 1 to the X parameter.

You could fix this using

 shield.transform.position = player.transform.position + new Vector3(0f,0f,1f);

but I much prefer @robertbu's answer of

 shield.transform.position = player.transform.position + player.transform.forward * dist;


avatar image whydoidoit · Oct 05, 2013 at 07:31 AM 0
Share

And just to point out that if you want to put something a number of degrees left or right of some vector on the current object, that is also easy:

    shield.transform.position = player.transform.position + Quaternion.AngleAxis(30, player.transform.up) * player.transform.forward;

Put the shield 30 degrees to the right of the player's current forward

0 Replies

· Add your reply
  • Sort: 

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

17 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

Related Questions

Local vs Global Transform 1 Answer

TransformPoint returns wrong points? 1 Answer

Rigidbody Constraints not work in local space(Unity 5) 0 Answers

How can I rotate one axis locally, and another globally? 0 Answers

Combining 2 Rotations: Global and Local 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