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 /
avatar image
0
Question by dane-wheaton · Sep 27, 2016 at 10:38 AM · 2ddirectiondistancevector2vector3.angle

Hop from transform to transform in 2D game?

Hi there, I have an array of transforms scattered around my scene, and I want my player to be able to move by jumping from one to the next (next meaning closest), in whichever direction they input. In the following function, I was able to get my guy to go from closest point to closest point, but I can't seem to figure out how to incorporate direction into it:

 Vector3 FindClosestPoint(Vector2 movementDirection)
     {
         Vector3 currentPosition = transform.position;
         Transform closestPoint = null;
         float distanceToClosestPoint = Mathf.Infinity;
 
         for (int i = 0; i < points.Length; i++)
         {
             Vector3 pointDirection = points[i].position - currentPosition;
             float pointAngle = Vector2.Angle(pointDirection, movementDirection);
             float distanceToPoint = Vector2.Distance(points[i].position, currentPosition);
 
             if (distanceToPoint < distanceToClosestPoint)
             {
                 if (pointAngle < 90)   // this is where I think my biggest logic error is
                     closestPoint = points[i];
                 distanceToClosestPoint = distanceToPoint;
             }
         }
         return closestPoint.position;
     }

The Vector2 param passed in is either Vector2.up, down, left, or right. I get a null reference exception when my "if (pointAngle < 90)" doesn't find anything - that if statement doesn't behave like I expect it to, instead of checking whether the angle between the direction I pass in and the direction of the target point is within my player's hardcoded 90 degree "cone of vision," it seems to be doing something else. I can never really predict which transform the player will jump to.

Comment
Add comment · Show 1
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 dane-wheaton · Sep 25, 2016 at 06:01 PM 0
Share

The first response in this forum is very close to what I have implemented, $$anonymous$$us the directional bit I'm trying to do:

http://forum.unity3d.com/threads/clean-est-way-to-find-nearest-object-of-many-c.44315/

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to compare several distances? 1 Answer

Top-Down Enemy Ai Animation 0 Answers

How can I raycast the direction my 2D character is facing? 1 Answer

[2D] Object at X distance to Y direction from point 1 Answer

Get distance between 2 gameObjects? 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