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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Dimwood2011 · Jan 31, 2013 at 01:13 AM · aiscipting

make enemy stop at distance and play animation

I want to keep the enemy above ground script.I like how it works.I want to add the STOP script to it.Combining the 2 scripts isn't an easy task for me.I get error after error.

enemy above ground script first

 var LookAtTarget:Transform;
 var speed : float = 3;  
 var controller:CharacterController;
     
 function Start()
 {   
     animation.Play("idle 1000");
     controller = gameObject.GetComponent(CharacterController);
 }
     
 function Update()
 {    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     transform.LookAt(Vector3(LookAtTarget.position.x, transform.position.y, LookAtTarget.position.z));  
         
     controller.SimpleMove(speed*transform.forward);
     animation.Play("walk 1000"); //You play the animation
 }

here is the Stop script

 var target : Transform;
 var moveSpeed = 0;
 var rotationSpeed = 3;
 var player : Transform;
 var distance:float = 5.8;
 var distanceAttack:float = 2.6;
 var myTransform : Transform;
 
 function Awake() {
 
     myTransform = transform;
 }
  
 function Start() {
 
     animation.Play("idle 1000");
     target = GameObject.FindWithTag("Player").transform;
 }
  
 function Update () {
     if(Vector3.Distance(transform.position, player.position) < distance){
         if(Vector3.Distance(transform.position, player.position) >distanceAttack){
         moveSpeed = 5;
         animation.Play("walk 1000");
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
     }

I added this snippet

 transform.LookAt(Vector3(LookAtTarget.position.x, transform.position.y, LookAtTarget.position.z));

into the above ground script.this helped with the player following the terrain on the y axis.

i cant seem to get enemy to follow me on y axis and stop at distance to play the animation that works in the STOP script.

Comment
Add comment · Show 3
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 Dimwood2011 · Jan 30, 2013 at 11:13 PM 0
Share

I could not get the rest of the code to format correctly sorry. my first time trying to use the format thingy.I left it where it could be edit if someone can fix this. Thanks dimwood

avatar image Qvintusdk · Jan 31, 2013 at 09:24 AM 0
Share

Just a question. What seems to be the problem? If the "STOP" script you have works why not use that rather than using both?

avatar image Dimwood2011 · Jan 31, 2013 at 02:02 PM 0
Share

the enemy will not follow the terrain properly.he goes through the ground. any ideas?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by deltamish · Jan 31, 2013 at 04:12 PM

HI I have answered a similar question Here

Comment
Add comment · Show 5 · 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 Dimwood2011 · Jan 31, 2013 at 05:09 PM 0
Share

thanks for the code deltamish.It is good code,but same problem is that the enemy goes through the ground.this is why i want to combine the 2 codes,or other suggestions would be appreciated.like setting up the collider for enemy not to go through the ground. thanks!!!I am not sure what i am missing.

avatar image deltamish · Feb 01, 2013 at 02:24 AM 0
Share

HI,Have you checked if terrian colider is added or not or if you are using a plane or a box then check if the colider has bbem added ot it or not.If you still have the problem then try removing the terrain and do it from begining.Have you added collider to the enemy character himself.Above all these if you face same problem

then

Create a empty gameobject and add a box colider to it and place it directly below the enemy character and make it a child of the enemy character

avatar image Dimwood2011 · Feb 01, 2013 at 03:23 AM 0
Share

the foot collider works with the enemy above ground script, but the STOP script he goes right through hills in ground.

avatar image deltamish · Feb 01, 2013 at 04:40 AM 0
Share

You might want to take out LookAtTarget.position.z cause i think (I have not opened unity yet)it controls forward rotation .This my completely turn your object which might be causing your objectto fall

avatar image Dimwood2011 · Feb 12, 2013 at 04:40 PM 0
Share

I ended up using the stop script only.I got rid of the character controller, and just used a capsule for the enemy.This worked.I think the two scripts were fighting each other.I gave enemy a rigidbody with contraints on the X Y Z rotation.I hope this helps others. TY Dimwood

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

12 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

Related Questions

Speed based on Distance 3 Answers

Enemy Attacks not lining up properly, why? 1 Answer

A* Pathfinding AIFollow in JavaScript? 0 Answers

How to reach multiple GameObjects' value , enemy AI 1 Answer

AI Field of vision 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