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 OGDE01 · Jan 26, 2019 at 06:56 PM · vector3aifloatforeachfor

Unity Is Caling My Vector3 a Float?

Hello, I am working on creating a basic BOIDs AI system in Unity and I am having an error. Here is my code:

         Collider[] colls = Physics.OverlapSphere(transform.position, 25f);
         Vector3 aiEpicenter = Vector3.zero;
         Vector3[] converted;
 
         // Move to epicenter
         for (int i = 0; i<colls.Length; i++) {
             foreach (Collider coll in colls) {
                 aiLocations.Add(coll.transform.position);
             }
             converted = aiLocations.ToArray();
             for (int b = 0; b<converted.Length; b++) {
                 converted[b] = new Vector3(converted[b].x / converted.Length, transform.position, converted[b].z / converted.Length);
             }
             for (int a = 0; a<converted.Length; a++) {
                 aiEpicenter = aiEpicenter + converted[i];
             }
         }
         transform.LookAt(aiEpicenter);
         transform.position += transform.forward * speed * Time.deltaTime;


For some reason in the inspector, though, it is saying Line 12 of the provided code is a float and therefore cannot be turned into a Vector3. If anyone could help, that would be greatly appreciated!

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
2

Answer by Bunny83 · Jan 26, 2019 at 07:23 PM

Your line reads:

 converted[b] = new Vector3(
     converted[b].x / converted.Length, 
     transform.position, 
     converted[b].z / converted.Length
 );

As you can see as the second argument to the Vector3 constructor you're passing transform.position (a Vector3) but a float value is required. You probably wanted to pass transform.position.y instead.


Though your whole approach is way too complicated. You can simply add up all positions and divide the result once at the end. There's no need for a List and another Array or two loops. Further more your outer loop is completely pointless. You're interating through all collisions already inside the loop. So you're doing the same things way too often. To find the center of the objects, just do:

 Collider[] colls = Physics.OverlapSphere(transform.position, 25f);
 Vector3 aiEpicenter = Vector3.zero;
 foreach (Collider coll in colls)
 {
     aiEpicenter += coll.transform.position;
 }
 aiEpicenter /= colls.Length;
 aiEpicenter.y = transform.position.y;
 
 transform.LookAt(aiEpicenter);
 transform.position += transform.forward * speed * Time.deltaTime;

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 Ymrasu · Jan 26, 2019 at 07:20 PM

 converted[b] = new Vector3(converted[b].x / converted.Length, transform.position, converted[b].z / converted.Length);

transform.position is a vector3, and it should be a float in that position. Did you mean to put transform.postion.y?

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

180 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 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 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

Operator cannot be used with left hand side of type.... 1 Answer

How do I perform math on Vector3.x or Vector4.x as if it were a float? 3 Answers

Direction and Distance from One Object to Another 1 Answer

How could I convert the y axis in a Vector 3 into a float? 1 Answer

Null Reference exception, in foreach loop in AI 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