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 KarlH · Apr 23, 2010 at 06:41 PM · aiproximity

getting enemys to follow character.

right iv got my enemy following my characters when he comes into range. im using two scripts:

var player : GameObject; var speed : float = 1;

function Start () { player = GameObject.FindGameObjectWithTag("Player");

 if (!player)
      Debug.Log ("ERROR could not find Player!");

}

function Update() { if (!player) return;

var distance = Vector3.Distance( player.transform.position, transform.position);

if ( distance < 20 ) { Debug.Log ("player is close");

   var delta = player.transform.position - transform.position;
   delta.Normalize();

   var moveSpeed = speed * Time.deltaTime;

   transform.position = transform.position + (delta * moveSpeed);

} else { Debug.Log("not close yet " + distance); }

}

and

var minimumRunSpeed = 2.0;

function Start () { // Set all animations to loop animation.wrapMode = WrapMode.Loop;

animation["idle"].layer = -1; animation["walk"].layer = -1;

animation.Stop();

}

function SetSpeed (speed : float) { if (speed > minimumRunSpeed) animation.CrossFade("walk"); else animation.CrossFade("idle"); }

my enemy follows me but im stuck with a few things, hoping someone can help me out. first of all the enemy doesn't face me, secondly i dont know how to get his animations playing (i have 2 animations idle, and walk) and he walks through objects too. all help 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

1 Reply

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

Answer by Ashkan_gc · Apr 23, 2010 at 06:49 PM

you can write a much simpler code

var player:GameObject;
function Update()
{
transform.LookAt (player.transform); //you look at player with this
transform.Translate (0,0,speed*Time.deltaTime); //moves with speed in local z and you are looking toward him so you move toward the player
//write other codes here
}

i don't understand your question about animations but you can start walk animation when you move with CrossFade method and start idle when you don't move.

Comment
Add comment · Show 3 · 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 KarlH · Apr 23, 2010 at 07:12 PM 0
Share

thanks for the quick reply. iv tried using the code but im getting an error at (4,18). iv got my character tagged as player. : /

avatar image KarlH · Apr 23, 2010 at 08:02 PM 0
Share

thank you! iv got it to work now i changed it to

var LookAtTarget:Transform; function Update() { transform.LookAt (LookAtTarget); //you look at player with this transform.Translate (0,0,5*Time.deltaTime); //moves with speed in local z and you are looking toward him so you move toward the player //write other codes here }

avatar image Ashkan_gc · Apr 24, 2010 at 05:33 AM 0
Share

oh yes i takes a transform as an argument.

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

Audio Source AI Attractor 2 Answers

How to make AI enemy do damage and only attack inside a distance or range 2 Answers

Detect player in range of enemy... 4 Answers

enemy waypoints and detection scipting 2 Answers

Enemy tank AI script TopDown 0 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