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
1
Question by Wes 1 · Jan 31, 2011 at 05:59 PM · arraylookat

Would someone tell me why this doesn't work?

I'm trying to wrap my head around Arrays, and I'm taking it one bite at a time, what I don't understand is why Transform.LookAt doesn't work with this, it seams like I'm grabbing one enemy in the array and telling the object to look at it, but that must not be correct.

function Update () { var Enemies : GameObject[]; Enemies = gameObject.FindGameObjectsWithTag("enemy");

var close = (Vector3.Distance(transform.position, Enemies[1].transform.position)> 1);

if(close){ transform.LookAt(Enemies[0]); }

}

So why doesn't this work?

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 Jessy · Jan 31, 2011 at 06:22 PM 0
Share

The console should be the one telling you why it doesn't work. Did the error message(s) not make sense to you?

2 Replies

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

Answer by Borgo · Jan 31, 2011 at 08:03 PM

(Maybe) Another problem
in the docs: function LookAt (target : Transform, worldUp : Vector3 = Vector3.up) : void

It needs to get a transform and your array is for GameObjects.
I can't try this now, but, try this too:

transform.LookAt(Enemies[0].transform);

Can you post the console error too?

Comment
Add comment · Show 1 · 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 Wes 1 · Jan 31, 2011 at 10:11 PM 0
Share

That was it! thanks, I gotta remember to focus on the basics.

avatar image
2

Answer by DaveA · Jan 31, 2011 at 06:14 PM

Enemies[0] would be the first, if only, such object found.

This line:

var close = (Vector3.Distance(transform.position, Enemies[1].transform.position)> 1);

results in a boolean (if the distance is > 1) So transform.LookAt(close) will fail, because it can't look at a boolean. Perhaps you want

if (close)
  transform.LookAt(Enemies[0].transform);

??

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

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

No one has followed this question yet.

Related Questions

Mega Bug In Turret - Help 5 Answers

Rotating object towards transform array 2 Answers

How can I make a GameObject face a GameObject with a specific Tag? 1 Answer

Instantiate same PreFab to an array of GameObject by RayCast. 1 Answer

Homing missile: choosing a new target 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