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 /
This question was closed Jan 30, 2021 at 12:30 PM by kuba86699 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by kuba86699 · Jan 30, 2021 at 09:24 AM · prefabtriggerpositionai

AI spawner problem

Hi, I have a simple "AI spawner" code:

using System.Collections; using System.Collections.Generic; using System.Threading; using UnityEngine; public class SpawnScript : MonoBehaviour { public GameObject botPrefab; GameObject botPref; public string botName; public bool inResp; public bool makeBot; void Start() { } void Update() { if (inResp == true) { makeBot = false; } else if (inResp == false) { makeBot = true; } if (makeBot == true) { botPref = Instantiate(botPrefab, this.transform.position + this.transform.forward, this.transform.rotation); botPref.name = botName; makeBot = false; } } void OnTriggerEnter(Collider other) { GameObject go = other.transform.gameObject; if (go != null) { inResp = true; } else { inResp = false; } } }

This works fine, but when creating a bot, bot transform.position.z always has +1 than transform.position.z spawner. For example, if the spawner's position is x = 500, y = 1, z = 500, when it creates a bot, it creates it at x = 500, y = 1, z = 501. As a result, it always creates several bots until it detects an object within itself (OnTriggerEnter).

How do I tell him to create a bot in himself? Spawner is a cube that is a trigger

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 Llama_w_2Ls · Jan 30, 2021 at 10:28 AM 0
Share

Could you post your code like this next time:

 using System.Collections;
 using System.Collections.Generic;
 using System.Threading;
 using UnityEngine;
 
 public class SpawnScript : $$anonymous$$onoBehaviour 
 { 
      public GameObject botPrefab;
      GameObject botPref;
      public string botName;
      public bool inResp;
      public bool makeBot;
 
      void Start() 
      { 
 
      } 
 
      void Update() 
      { 
           if (inResp == true) { makeBot = false; }
           else if (inResp == false) { makeBot = true; }
 
          if (makeBot == true) 
         { 
               botPref = Instantiate(botPrefab, this.transform.position + 
               this.transform.forward, this.transform.rotation);
 
              botPref.name = botName;
              makeBot = false;
         } 
     } 
 
     void OnTriggerEnter(Collider other) 
     { 
            GameObject go = other.transform.gameObject;
 
            if (go != null) { inResp = true; } 
           else { inResp = false; } 
      } 
 }

2 Replies

  • Sort: 
avatar image
0

Answer by Llama_w_2Ls · Jan 30, 2021 at 10:30 AM

Could you reset the transform of botPrefab? Possibly the prefab itself in one forward on the z axis, causing it to look like it spawns at 501, instead of 500.

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 kuba86699 · Jan 30, 2021 at 10:42 AM 0
Share

All of the axes (x, y, z) for the bot prefab are zeroed, the problem still exists

avatar image
0

Answer by kuba86699 · Jan 30, 2021 at 12:15 PM

I was able to fix the problem by adding this line of code

botPref.transform.position = this.transform.position;

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

Follow this Question

Answers Answers and Comments

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

Prefab instantiated off screen problem 0 Answers

How can I spawn a prefab relative to a cube position? 1 Answer

Instantiating an Object doesn't use the given position. 1 Answer

How to create waypoint between two gameobjects according to variable value? 0 Answers

Prefabs and NavMesh 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