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 /
  • Help Room /
avatar image
0
Question by captainfrogduh · Sep 01, 2015 at 12:35 PM · unity 5transform.position

Convert transform.position to Local Space

I need to convert transform.position.y to compare with Camera.main.pixelHeight/2. My code is if (transform.position.y>Camera.main.pixelHeight/2) but of course I cant get the required result because Im trying to compare a World Space Coordinate with a Local Space Coordinate. Any help would be appreciated.

Comment
Add comment · 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 Scribe · Sep 01, 2015 at 12:42 PM 1
Share

Transform.TransformPoint and Transform.InverseTransformPoint are what you are likely looking for.

Though maybe you are trying to check whether and object is on screen? in which case you can use Camera.WorldToScreenPoint or Camera.WorldToViewportPoint

avatar image captainfrogduh · Sep 01, 2015 at 12:50 PM 0
Share

@Scribe Im just trying to add some enemies, when they enter the screen view from the top they are moving at a slower speed. Then when they cross the half height of the screen(going downwards) they move at a higher speed and leave the screen from below. This was done in a game called Ge$$anonymous$$i for iOS and Android.

avatar image Scribe · Sep 01, 2015 at 02:47 PM 1
Share

If you want the position they are on screen you should use the second functions I mentioned:

 Vector3 screenPos = Camera.main.WorldToViewportPoint(yourEnemy.transform.position);
 
 if(screenPos.y > 0.5){
   //do something
 }else{
   //do something else
 }
avatar image captainfrogduh · Sep 01, 2015 at 06:54 PM 0
Share

@Scribe Thanks man! Works perfectly :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Landern · Sep 01, 2015 at 12:41 PM

Transform.InverseTransformPoint, the example should be enough to get you going from the script API.

Comment
Add comment · Show 4 · 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 captainfrogduh · Sep 01, 2015 at 01:02 PM 0
Share

@Landern Does not work. Whats wrong :(

 int screenHeight = Camera.main.pixelHeight;
 Vector3 targetPos = new Vector3(transform.position.x,-10.0f,0f);
 Vector3 convertWorldToLocalPos = transform.InverseTransformPoint(transform.position.x, transform.position.y, transform.position.z);
 if (convertWorldToLocalPos.y>screenHeight/2) {
     float move = slowSpeed * Time.deltaTime;
     transform.position = Vector3.$$anonymous$$oveTowards(transform.position, targetPos, slowSpeed*Time.deltaTime);
 } else if (convertWorldToLocalPos.y<screenHeight/2) {
     float move = fastSpeed * Time.deltaTime;
     transform.position = Vector3.$$anonymous$$oveTowards(transform.position, targetPos, fastSpeed*Time.deltaTime);
 }
avatar image captainfrogduh · Sep 01, 2015 at 01:06 PM 0
Share

Btw this is in Update()

avatar image Landern · Sep 01, 2015 at 01:40 PM 0
Share

Use the cameras transform, check out the API. Also you're not doing anything interesting to the transforms in the 3 parameters override, so use the one that only takes the full Vector3 as below. Also base it off the Camera's transform.

Yours:

 Vector3 convertWorldToLocalPos = transform.InverseTransformPoint(transform.position.x, transform.position.y, transform.position.z);

$$anonymous$$ine, may work?:

 Vector3 convertWorldToLocalPos = Camera.main.transform.InverseTransformPoint(transform.position);


avatar image captainfrogduh · Sep 01, 2015 at 06:56 PM 0
Share

@Landern for some reason it doesn't work. Im not quite sure why but WorldToViewportPoint as mentioned by Scribe works perfectly. Im sure Im making a mistake somewhere with InverseTransformPoint but anyways thanks for help :)

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

31 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

Related Questions

How to attach an object to another object so that moving one moves the other 0 Answers

Get Random transform point inside collider 0 Answers

make a gameobject move to another gameobject that was selected by mouse click? 1 Answer

Trying to transform my character's local position on a GetKey. 0 Answers

Instantiated Players Transform Not Changing 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