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
0
Question by Randize · Dec 26, 2013 at 11:55 AM · listsort

Switching Target during Target Lock

I have a working locking targeting system for a third person game and it's selecting the nearest target from the sorted list when it's triggered.

However I'm thinking to add a feature where player can switch the target with the right analog stick, so when the stick is push left, the selected target will shift starting from the nearest left of the selected target from the player view and vise versa when the stick is push to the other side. (Much like how Darksiders would)

Can't figure out how to sort the list.

For the nearest sorting, I'm using delegate with CompareTo wondering if there's any similar way I can achieve this.

targets.Sort(delegate (Transform t1, Transform t2) { return Vector3.Distance (t1.position, followXform.position).CompareTo(Vector3.Distance (t2.position, followXform.position)); });

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 Randize · Dec 26, 2013 at 03:22 PM 0
Share

By the way followXform is just the instance of the character's transform.

1 Reply

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

Answer by sparkzbarca · Dec 27, 2013 at 02:33 PM

i'd just spherecast off to the side of the player with a reasonable radius and sort the list returned by distance to player and give them the target with the shortest distance.

as long as your not allowing someone to switch to a very distant target this method is just easier and more intuitive.

since you already have a sorted by closest list another possibility is to simply convert each enemy transform position into local co-ordinates using a foreach loop.

that will give you each objects position with 0 on the X axis being the player so

if they flick left go through the list of sorted distance and transform into a sorted local space distance.

then foreach through the list if the X value of the transform.position is < 0 (so its left of the player) return it

if they flick right

same thing but

if the X value is > 0 return it.

actually jsut one foreach

convert and then dont store it or anything just go through each cause there sorted by distance already

convert to local space

if greater than 0 on X toss it back for right flick if less than 0 on X toss it back fro left flick

Comment
Add comment · Show 9 · 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 Randize · Dec 27, 2013 at 05:33 PM 0
Share

Does spherecasting expensive to do every frame update?

For clarification, my list is sorted by distance from the targets to the players.

However, as per Darksiders target switching system, the list seems to be updated every frame (responsive to the enemy that moves from left to right and vice versa), and if you flick left, you may only select the targets that exist to the left of the selected target. So my guess is that, there are 2 lists created for the left and right sides, sorted by distance from the selected targets to the furthest targets. If this is a possible way to do it, I'd like to get some help with the list generation. Shperecasting is the way to go? Any other alternative?

I don't seem to quit get the second alternative, if that's relevant now, I'd love to explore it.

avatar image sparkzbarca · Dec 27, 2013 at 06:57 PM 0
Share

so the way the second option works is hopefully you understand local space right, which is just a way of making the center of the "world" be that object, so an enemy object that you transform the position of what your saying is

I dont care what it thinks up is I don't care what the world as a whole thinks up is. I want you to put me "the player" as the center of the world and use my personal axes and tell me where he is relative to me.

So for example

i made you a quick example in paint of what that means

https://www.dropbox.com/s/k4pd8sssdf2iako/Screenshot%202013-12-27%2012.49.53.png

so now as you can see the thing is once the word is centered on the player. Then the X axis is the players X axis, its the players left and right, so if in the players local space your position on the X axis is less than your left and if its greater than its right, thats just because of convention of course which is that numbers go up in the right direction and decrease in the left direction, so we exploit that by checking to see if once they've been put in player space an object is left or right.

Heres a code example of that in action with a sorted by distance list.

 list SortedList;
 vector3 LocalPostion;
 foreach(gameobject enemy in SortedList)
 {
 LocalPosition = player.transform.transformpostition(enemy.transform.position);
 
 //if its X is negative in local space so its left
 if( (LocalPosition.x < 0) && FLickedLeft)
 return enemy;
 
 //we already know its not on top of the player and its not left so

`` we can just assume its right with an else, if you want to be super secure you could do a check to make sure its positive and he isn't in fact like directly above/below him for some reason in which case just

an else if ins$$anonymous$$d of just an else

 else 
 
 //he is right so
 if(FlickedRight)
 return enemy
 }
avatar image Randize · Dec 27, 2013 at 07:12 PM 0
Share

Great would love to try this. How would I get the position of the enemy relative to the player's local space? Any function I can use to do this?

avatar image sparkzbarca · Dec 27, 2013 at 07:26 PM 0
Share

yea, actually i gave it to you slightly wrong in my code but the code is

localpostioin = player.transform.inversetransformpoint(enemy.transform.position)

inversetransformpoint(vector3 world) takes a world position/point and returns its position in local space

so just in that code above put the correct line using inversetransformpoint.

just so you know transformpoint, transformdirection work for positions and direction in local space to go global and inverse takes globals and makes them local

avatar image Randize · Dec 27, 2013 at 07:32 PM 0
Share

Thanks man, it'll take me a while dwelling into InverseTransformPoint, this is a new concept for me, will post my finding soon. Thanks again! :)

Show more comments

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

19 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

Related Questions

A node in a childnode? 1 Answer

Show Top 10 Of 1 GameType 2 Answers

Sorting a list of GameObjects by accessing their int values 2 Answers

Sorting Game Object Name In Numerical and Alphabetical Order via List 2 Answers

Sorting Variables Help 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