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 Kyle 11 · May 03, 2011 at 01:01 PM · javascriptaispawntargeting

Ai finding target after spawning

Edit* Updated the code with the now Working code for others to look at. )

ok, basically the problem I'm having is that there seems to be no way of getting the AI to target the player after it spawns. I've tried having it find the player tag, but unity always says that transform and gameobject can't work together. basically i just need the enemy to automatically find the player and attack. If anyone could help it would be greatly appreciated.

heres the code

static var health : float = 100.0; var player = transform; var speed = 5.0; var chaseRange = 50.0; var force = 1; private var controller : CharacterController;

function Awake(){

 player = GameObject.FindWithTag("Player").transform;

}

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

function Update() { if (player == null) animation.CrossFade ("Idle"); else;

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


 if (range <= chaseRange)
 {
     transform.LookAt(player);
     var moveDirection : Vector3 = transform.TransformDirection(Vector3.forward);
     controller.Move(moveDirection * Time.deltaTime * speed);
     animation.CrossFade ("Walk");
 }

}

function OnTriggerEnter(collision : Collider) { if(collision.gameObject.tag == "Player"){ transform.Translate(0, 5, -3); animation.CrossFade ("Lunge"); //print("push back"); } }

function OnCollisionEnter(collision : Collision){ //If it collides with the player's bullet, run the apply damage function if(collision.gameObject.tag == "BulletPlayer"){ ApplyDamage(); print("hit"); } }

var hitPoints = 10;

function ApplyDamage(){ //subtract 5 hitPoints hitPoints -= 5; //if the remaining hitPoints are more than zero, do nothing if(hitPoints > 0){ return; } //if the remaining hitPoints are less than zero, run the die function if(hitPoints <= 0){ Die(); } }

function Die(){ Destroy(gameObject); }

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
0
Best Answer

Answer by Persona · May 03, 2011 at 06:34 PM

Add a function Awake to search for the tag player:

function Awake(){
player = GameObject.FindWithTag("Player").transform;
}

That way, the moment it spawns, the player is set.

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 Kyle 11 · May 04, 2011 at 11:29 AM 0
Share

Thanks, i was really tired that day and kept putting this in a start function.

avatar image
0

Answer by red 2 · May 03, 2011 at 03:11 PM

check the FPS tutorial, it features an AI that moves around til it sees the player, then attacks. you might want to use it as a base for your own

http://unity3d.com/support/resources/tutorials/fpstutorial

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

No one has followed this question yet.

Related Questions

random target selection 2 Answers

Can't make enemy respawn 0 Answers

Can't do spawning 0 Answers

AI moving forward without spawnpoints? 2 Answers

What would be the best way to setup Target priority 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