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 Shaun 1 · May 07, 2011 at 07:34 PM · gameobjectaidistancetagfindwithtag

Distance thing with gameobject ?

hello guys, i want my AI's not to be too closer , so i made a code like this in the AI:

function Update() {

var friend = GameObject.FindWithTag("UnGoro");

WHAT SHOULD I PUT HERE TO MAKE MY AI TO PUT A DISTANCE AT LEAST 10 BETWEEN HIS FRIEND? }

please help :)

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by GesterX · May 07, 2011 at 07:42 PM

Distance is used to get the distance between two objects - not change them. Your error is coming from the fact that you need to do

friend.transform.position

Your script (this should be attached to your AI) should do something like this:

function Update()
{
if (Vector3.Distance(transform.position,friend.transform.position) > 10)
{
//some code to move the ai towards your player
}
else
{
//stop the ai
}
}
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 Shaun 1 · May 07, 2011 at 07:47 PM

actually the thing i want to make is different , i edited it , i don't want to make if statement or make the AI to move toward my player.

I'm something like observer in the scene, i have 10 characters in group A 10 characters in group B fighting with each other with swords.

I want group A characters to have at least 10 meters between each other.

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 GesterX · May 07, 2011 at 07:53 PM 1
Share

Okay well then just modify my answer to suit your needs... Also don't post comments as answers!

avatar image
0

Answer by Tommy · May 07, 2011 at 07:48 PM

I belive that "friend" already is a default var by Unity, try changing you var name.

Also, you'll need to do like:

var friend = GameObject.FindWithTag("UnGoro");

function Update() {

if (Vector3.Distance(transform.position,friend.position) > 10) { //Move the object 10, maybe using instanitate or something, the ofcourse can't SEE it walk }

Maybe you can attach waypoints to the players, that are as far as tou like away from the player, and the other players follow those waypoints.

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 Matheusfig · Nov 18, 2012 at 03:02 PM

Put This Script In Your Friend: Put the tag Player in your player and don't put your player in the "var Player : Transform".

 var Player : Transform;
 var MoveSpeed = 10.0;
 private var StopSpeed = 0.0;
 var RangeToStop = 10.0;
 var damp = 5;
 
 function Update () {
 Player = GameObject.FindWithTag("Player").transform;
 
 if(Vector3.Distance(Player.position,transform.position) > RangeToStop){
    var rotate = Quaternion.LookRotation(Player.position - transform.position);
    transform.rotation = Quaternion.Slerp(transform.rotation, rotate,damp * Time.deltaTime);
    transform.Translate(0,0, MoveSpeed * Time.deltaTime);
   }else{
   if(Vector3.Distance(Player.position,transform.position) < RangeToStop){
    var rotate2 = Quaternion.LookRotation(Player.position - transform.position);
    transform.rotation = Quaternion.Slerp(transform.rotation, rotate2,damp * Time.deltaTime);
    transform.Translate(0,0, StopSpeed * 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

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

1 Person is following this question.

avatar image

Related Questions

Find with tag problem 1 Answer

Make Navmesh agent follow closest object with tag 0 Answers

Tag is not defined, when it has been manually created? 1 Answer

difference between GameObject.FindGameObjectWithTag(Tag tag) and GameObject.FindWithTag(Tag tag) 3 Answers

lookAt nearest object to another object 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