• 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 Henrique Dantas · Jan 29, 2011 at 09:24 PM · aifollow

Simple AI Scripting

Hey, i'm making an island game and there is one part of the game, that a Jaguar chases you to kill you. But, i didn't make it yet, i don't know how to make a simple AI script that needs to be like this: if you are in a pre-determinated range, the jaguar starts to chase the player and if the player is reached by the Jaguar it will die, but if the player escapes the Jaguar stops to chase him. Thank's to all.

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 Sachin070402 · Jul 22, 2015 at 07:19 PM 0
Share

love jaguars

2 Replies

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

Answer by anomalous_underdog · Jan 29, 2011 at 11:22 PM

I'm assuming the jaguar uses a character controller. Use Transform.LookAt to make the jaguar face where the player is, but check the jaguar's distance to the player using Vector3.Distance and check if it is within a certain value. If it is, make the jaguar face him using Transform.LookAt, and make it move in its local forward direction. Use

Vector3 moveDirection = transform.TransformDirection(Vector3.forward);

to get the local forward direction. Use CharacterController.Move to actually move the jaguar.

Also check the value of the earlier Vector3.Distance again. If it is within a smaller value, call Destroy(gameObject, 1.0f) to delete it. The 1.0 means it will wait for 1 second before actually being deleted. Use that time to have it play its death animation perhaps.

static var health : float = 100.0; var player : Transform; var speed = 5.0; var chaseRange = 50.0; var dieRange = 5.0; private var controller : CharacterController;

function Start() { controller = GetComponent(CharacterController); }

function Update() { if (player == null) return;

 var range = Vector3.Distance(player.position, transform.position);
 if (range <= dieRange)
 {
     Destroy(player.gameObject);
 }
 else if (range <= chaseRange)
 {
     transform.LookAt(player);
     var moveDirection : Vector3 = transform.TransformDirection(Vector3.forward);
     controller.Move(moveDirection * Time.deltaTime * speed);
 }

}

Comment
Add comment · Show 9 · 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 Henrique Dantas · Jan 30, 2011 at 12:26 AM 0
Share

Thank's i'll try that and if it works i'll accept your answer.

avatar image Henrique Dantas · Jan 30, 2011 at 01:50 PM 0
Share

I'm trying to do this with the following script:

static var health : float = 100.0; var player = Transform;

function Update () {

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

if(range <= 50){

 transform.LookAt(player);

}

}

but Unity gives the errors: BCE0019 at line 6: 'position' is not a member of 'System.Type'. and BCE0023 at line 10: no appropriate version of UnityEngine.Transform.LookAt for the argument list ('System.Type') was found.

What is my mistake? I can't see it

avatar image Henrique Dantas · Jan 31, 2011 at 12:59 AM 0
Share

i foud my problem, the correct variable is: var player : Transform; Sorry, i'm continuing to make the script.

avatar image Henrique Dantas · Jan 31, 2011 at 01:30 AM 0
Share

@anomaluos_underdog, thanks to your help, but i can't figure out how to do this: when the player is in jaguar's range, the jaguar will chase him. i don't now how to move the jaguar to the player's position in a deter$$anonymous$$ated velocity. Thanks again.

avatar image anomalous_underdog · Feb 01, 2011 at 07:31 PM 0
Share

@Henrique: Try the code.

Show more comments
avatar image
0

Answer by Brian5cbis · Jul 31, 2013 at 03:32 AM

This helped me a lot! Thanks ^^

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Make Clone AIs Stay Apart? 1 Answer

Enemy following the target with ITween 2 Answers

Enemy Follow Script Help 2 Answers

AI enemy Stuck at corners 2 Answers

Set navmesh destination if to object that is in range 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