Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by segasega89 · Feb 04, 2021 at 12:25 PM · world spacelocalspace

Help with trying to understand transform.TransformDirection method

Hi there,

I'm following a beginners tutorial about raycasts at the moment which consists of a turret that the player is able to rotate left and right on the z-axis and fire a raycast from the top of the turret towards three possible targets. When the raycast has hit one of the targets it outputs a message in the console stating which target was hit. Here is a video of what the end result of the tutorial looks like:

https://www.youtube.com/watch?v=PtI51DRvKEI&ab_channel=segoviate

And here is the code shown below here:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
  
 public class Gun : MonoBehaviour
 {
  
     private float _moveInput;
     public LayerMask target;
  
  
     // Update is called once per frame
     void Update()
     {
         _moveInput = Input.GetAxis("Horizontal");
         transform.Rotate(0, 0, -_moveInput);
  
         if (Input.GetKeyDown(KeyCode.Space))
         {
             RaycastHit2D hit = Physics2D.Raycast(transform.position,transform.TransformDirection(Vector2.up),10f,target);
             Debug.DrawRay(transform.position, transform.TransformDirection(Vector2.up) * 10f, Color.red);
  
             if (hit)
             {
                 Debug.Log(hit.collider.name + " has been hit with a raycast!");
             }
         }
     }
 }
  

As you can see in the above code the tutor uses a thing called "Transform.TransformDirection" which I don't understand fully. My basic understanding of this function is that it takes the co-ordinate values that is stored within a vector and translates them to world space? If I was to replace the second parameter in the Physics2D.Raycast method in the if statement with just "Vector2.up" the raycast just fires straight up regardless of the z-value of the "local rotation" of the player game object. I don't understand why this is? It seems that the Transform.TransformDirection function makes sure the origin of the raycast will start at the top of the turret in the direction that it is pointing towards.

I've been looking at videos on Youtube about the difference between local and world space but it still doesn't help me understand the above example I have described.

Any helpful information about my question would be appreciated :)

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

Answer by unity_ek98vnTRplGj8Q · Feb 04, 2021 at 06:19 PM

So local space means relative to a specific object while world space means what Unity sees.


Imagine you are making a game about astronauts floating around in space. You want an astronaut to shoot a laser up over his head, but how do you actually do that? You need to know the direction to shoot the laser in, and that direction relative to the astronaut is just "up", or in Vector terms that direction is (0, 1, 0). But if this astronaut is floating around in space and rotating every which way, you need to know what the ACTUAL direction is relative to the X, Y, and Z axis in the Unity engine. If the astronaut's head is pointing along the X axis, then "up" for the astronaut is actually "right" according to Unity world coordinate system.


Transform.TransformDirection basically is just a function where you say "OK, I know the direction I want relative to my astronaut, can you please tell me what the direction is relative to the Unity Coord System so that I can do something useful with it. So if you astronaut's head is pointing along the Unity X axis, you can say transform.TransformDirection(Vector3.Up) and Unity will tell you "Oh, the actual direction for this is along the X axis".


Transform.Transform direction is more useful for more complicated relative vectors, since for the basic directions (up, forward, right) you can just call transform.up, transform.forward, and transform.right.

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

110 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 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 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 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 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

Move camera along 2 axes in WorldSpace 2 Answers

Can I use the local x,y transforms of a plane? 1 Answer

Random.insideUnitSphere doesn't work as expected 0 Answers

Look at around one axis in local space 1 Answer

Parenting World Space Particle to a Bone 1 Answer


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