• 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 Takamori · Jan 14, 2013 at 03:06 AM · projectiletransformdirection

Why should I use TransformDirection function for firing projectiles?

I'm playing around with a tutorial on firing a projectile and I came across a bewildering issue. It would seem that my understanding of Local and World Space is opposite to what they truly are.

This is what I know about Local and World Spaces.

World Space is referenced from the World Origin (0,0,0), i.e Static and always the same direction

Local Space is referenced from the Game Objects Origin(Center of the Object). i.e Dynamic and changes orientation as the Object is rotated.

From this understanding it would seem to me that World Space Axis's are the same for all objects in the Scene regardless of their rotations or positions since it is referenced from the world origin and not the individual object's origin. So regardless of whether i rotate my character 90 degrees or 120 degrees, The projectiles should still fire in the same direction using World Space, in this case the World's z-axis.

It would seem that if I wanted my projectiles to fire along my characters local z-axis's I would want to use Local Space Coordinates. That way my projectiles fire from the front of my character at all times, even if it is rotated in different directions.

This is the line of code this is bothering me.

clone.rigidbody.velocity = transform.TransformDirection(Vector3 (0,0,speed));

the clone variable is a GameObject that I instantiate as my projectile when i click the Fire1 button. Now the tutorial says to use the TransformDirection function to convert from Local Space to World Space.

when i run the script using Transform.Direction, i.e. using World Space, it works perfectly. when i click Fire1, it fires from the front of my character no matter where it is looking (Which I thought was what Local Space was for). When i remove the TransformDirection function from the line of code, i.e using Local Space, my character fires in the same direction regardless of where it is aiming (Which I thought was what World Space was for).

So the results of running my script are the exact opposite of what I thought i knew about Local vs World Space. I could just accept what the tutorial says to do but I would rather rectify my misunderstanding on this concept for future projects. If anyone can help clarify this for me I would greatly appreciate it.

BTW I'm an engineer and quite familiar with the concept of vectors and coordinate planes etc... Or at least i thought i was =P I must be either making a stupid mistake with this or perhaps its a project setting in Unity?

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

2 Replies

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

Answer by Owen-Reynolds · Jan 14, 2013 at 04:25 AM

Local Space is a shortcut -- really nice to say things like "3 meters to my left". But, Unity can only do things "for real" in standard (x,y,z) coords, which is World Space.

A tricky part is, does 5 mean five feet or 5 yards? Neither/both. You have to say. Likewise (2,0,5) is local or world depending how you think of it.

If you're "thinking" feet when you say 5, you have to convert to 1-2/3s yards when ordering carpet. Likewise, if you're thinking "2 to my right and 5 ahead" for (2,0,5) then you're using Local, so have to ask Unity to please compute the actual (x,y,z) world numbers where that is. Put another way, if in your mind (2,0,0) means East, then you're done (since you were thinking World.) If you were thinking My-Right, then you have to figure out the real NESW to go there.

A last trick is that transform.forward is in world coords??? That seems to make no sense, since "1 ahead of me" is clearly local. What it really means is to first think "1 ahead of me," step there and then convert that to the real (x,y,z) so you can use it(*).

In TransformDirection, think of Transform as a noun, not a verb. fred.TransformDirection(0,0,5) means to go +5 in some Z. Going 5 North would be pointless (I can compute Z+5 myself.) Ah ... it means to go 5 in the facing direction of the Transform, fred. If we ask where that is and were told "it's 5 ahead of fred" that would just be pointless. Clearly, the answer should be the standard (x,y,z) where that puts you.

(*) transform.forward isn't really 1 ahead of you. It's the real, small (x,y,z) to move from you to 1 ahead of you.

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 Takamori · Jan 14, 2013 at 04:37 AM 0
Share

Ah I understand. Thank you very much. While Owens answer really solidified my understanding of this issue, Eric's and Lovrenc's answers were also very helpfull is getting the "Big Picture" here

@ Eric5h5 - Thank you for suggesting transform.forwards and mentioning that rigidbody was in world space.

@ Lovrenc - Thank you for pointing out where my mistake in thinking was.

Thank You for your speedy responses. It never ceases to amaze me how friendly and helpful the Unity community is. =]

avatar image
0

Answer by Eric5h5 · Jan 14, 2013 at 03:09 AM

In fact you don't need TransformDirection for this, just use

 clone.rigidbody.velocity = transform.forward * speed;

Anyway, rigidbody.velocity is world space, so you need to convert local space to world space in order to get the velocity to go in the right direction.

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.

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

11 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

Related Questions

Lock On Projectile 4 Answers

Shoot Not Letting Me Shoot Twice 0 Answers

How do make a projectile that pushes its target when it hits? 2 Answers

Spawn a bullet of a cannon at right position 1 Answer

Velocity and AddForce Problems 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