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 Malebolgia · Jul 27, 2016 at 12:23 PM · gameobjecttransform.positiondirectionanglewrong

Very weird. Transform position is at two places at the same time.

So, i'm trying to get a direction, but somehow it's in the completely wrong position, seeming to be endlessly far away.

My gameobjects also show up in the sky, but i think that is just my other cameras seeing them, because i couldn't find any gameobjects there, yet the target direction seems to point that way.

I hope you can shed some light on my situation. By looking at this piece of code and a picture it might make more sense.

Here are the variables

             GameObject current_enemy;
             self = transform.position + new Vector3(0f, 1f, 0f);
             float angleToEnemy;
             Vector3 targetDirection;

This is where i get the direction to the target.

     void CheckIfFacingEnemy()  {

         targetDirection = self + current_enemy.transform.position;
         angleToEnemy = Vector3.Angle(targetDirection, transform.forward);
         if(angleToEnemy < 40)   {      _facingEnemy = true;        }
         else                    {      _facingEnemy = false;        }
     }

and heres gizmos

     void OnDrawGizmosSelected()  {
 
         Gizmos.color = Color.red;
         Gizmos.DrawRay(transform.position + new Vector3(0, 1, 0), targetDirection);
         Gizmos.DrawSphere(current_enemy.transform.position, 2);
     }

and this picture show that the current enemy is at the right place on the ground, but also showing up in the sky, where it seems to point...

Keep in mind that i can't find any gameobjects in the sky where you can see the reflections. alt text

The only thing i know is that my confusion level is way over 9000.

If i didn't provide enough information, please let me know.

problem.png (485.8 kB)
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
1
Best Answer

Answer by Soos621 · Jul 27, 2016 at 01:31 PM

Your target direction should just be:

Vector3 targetDirection = otherObject.position - thisObject.position;

You don't need your origin because you are raycasting from the point of origin towards the direction but always subtract the vector3 origin point from the target vector3

Comment
Add comment · Show 2 · 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 Soos621 · Jul 27, 2016 at 01:38 PM 0
Share

As a note you can also find out if one object is facing the other with vector3.dot example

Vector3 direction = target.transform.position - this.transform.position;

Float dProduct = Vector3.dot(this.tranform.forward, direction);

If(dProduct > .5){ // Is facing object }

avatar image Malebolgia · Jul 27, 2016 at 01:54 PM 0
Share

Trying it this way made me realize that the problem was my Self variable. I provided bad code, because the Self variable was from other script, and i had a Self variable in this script as well, but i had not assigned it so it was vector 3.zero. This help room is brilliant! Thank you!

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

76 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

Related Questions

Movement and Control Problems with understanding 2 Answers

SetActive canvas relative to selected object 0 Answers

Move GameObject in a direction of a mouse but longer 1 Answer

Struggling to get the rotation the player is moving in. 1 Answer

Moving a GameObject in an Ellipsoid path 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