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 kamenjhet · Jan 27, 2013 at 10:42 PM · arraytargetcyclenext

Cycle through targets.

Well, I have a newbie problem here.

I have my character looking at a fixed enemy (enemy1; there are 3 enemies in the scene) and I want to press a key to look at the next enemy. Basicly, I want to cycle through them but I have a problem. I have absolutely no idea how to do it. I know it probably has to do with arrays but the docs and "tutorials" all seem to be aimed at people with previous experience, not at newbies. So, not only I need help with this specific thing, I also need to know where to find docs aimed at total newbies with no experience in this.

This is the part of my code dealing with targeting and "orbiting" the enemies.

 var target : Transform;
 var speed = 5.0;
 
 function Update () {
 transform.LookAt(target);
 transform.eulerAngles.x=0;
 transform.eulerAngles.z=0;
 var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
 var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
 transform.Translate(x, 0, z);
 }
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

2 Replies

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

Answer by Gizmoi · Jan 28, 2013 at 01:47 AM

I'm not sure of the exact syntax as I use C# myself. But, you should change target to an array and assign via the inspector / find in scene all 3 enemies.
You would then have an int denoting which was the current enemy.
You would access target using this int and when pressing left and right increase and decrease the int, making sure it does not go below 0 or above the number or enemies in the array.

I believe simply changing the type of target to Transform[] will make it an array and if it is public and serialised to the inspector you can define its size there and drop on each enemy.

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
avatar image
0

Answer by kamenjhet · Jan 28, 2013 at 09:15 PM

Thanks for the quick answer. Turning the Transform into an array worked perfectly but now it's causing a problem I didn't have before. With my previous code, my character would be constantly focusing on a fixed enemy. I could move around all I wanted and it would still be looking at the enemy. Basicly, I would be "orbiting" the enemy. Now that doesn't happen anymore. I have 3 enemies and when I press the "nexttarget" key, it looks at the next target and thats good but it doesn't look at it all the time. When I switch target, it looks at the next target but it stops looking at it as soon as I start moving. How can I make it so that it keeps looking at the target even after I start moving around? This is my current code:

 var target : Transform[];
 var speed = 5.0;
 var targetnumber : int;
 
 
 
 
 
 function Update () {
 
 transform.eulerAngles.x=0;
 transform.eulerAngles.z=0;
 var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
 var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
 transform.Translate(x, 0, z);
 
 
 
 if (Input.GetButtonDown ("nexttarget")) {
 targetnumber++;
 
 if (targetnumber > 2) targetnumber = 0;
 
 if (targetnumber == 0) {
 transform.LookAt(target[0]);
 }
 
 if (targetnumber == 1) {
 transform.LookAt(target[1]);
 
 }
 
 if (targetnumber == 2) {
 transform.LookAt(target[2]);
 
 }
 
 }
 
 
 }
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 Gizmoi · Jan 28, 2013 at 10:08 PM 0
Share

This should really be posted as comment rather than a new answer :P

$$anonymous$$ove the last 3 if statement outside the first if. You're only telling your player to 'LookAt' your targets when you press the next target button. $$anonymous$$ove the ifs outside and it will call the LookAt every frame.

Also, for tidyness, you can use targetnumber as the array indexer. i.e.

 transform.LookAt(target[targetnumber]);

This will then also work if you choose to add more targets in the inspector. However you will have to change the 2 limit to whatever the length of the array is - target.Length.

Hope this helps and I haven't thrown too much information at you :)

avatar image kamenjhet · Jan 29, 2013 at 01:57 AM 0
Share

Problem solved. 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

10 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

Related Questions

Accessing boolean properties from array of transforms 1 Answer

How to apply variable to next item in array? 1 Answer

Pick the oldest object from an array. 2 Answers

What is wrong with this code? 0 Answers

Move from one target to another 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