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 Great Alexander · Jan 07, 2013 at 11:39 AM · rotationaitarget

How can I stop a character from rotating around when it reaches a aspecific point?

I am trying to use some AI but there`s a problem which is the character keep looking backward and forward fast when it reaches to the enemy point

Here is what I`ve done using UnityEngine; using System.Collections;

 public class EnemyAI : MonoBehaviour {
     
     Transform target;
     int rotationSpeed;
     int moveSpeed;
     
     
     // Use this for initialization
     void Start () {
         
         GameObject go = GameObject.FindGameObjectWithTag("Player");
         target = go.transform;
         
         rotationSpeed = 28;
         moveSpeed = 2;
         
     
     }
     
     // Update is called once per frame
     void Update () {
         
         transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation (target.position - transform.position),
         rotationSpeed );
                 
         transform.position += transform.forward  *moveSpeed * Time.deltaTime;
         
         
     }
     
     
 }
   
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

3 Replies

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

Answer by $$anonymous$$ · Jan 07, 2013 at 12:18 PM

It's not clear from the question whether your problem is with your character or with your enemy, but after trying, probably both. It's because the enemy moves "inside" your player, they are colliding and that leads to weird stuff.

a) One solution is that you set up a follow distance so the enemy doesn't get too close to the player, something like this:

 public float followDistance = 5f;
 bool canMove = false;
 ...
 void Update() {
 // rotation can stay where it is
 if (canMove)
     transform.position += transform.forward  * moveSpeed * Time.deltaTime;
 
 if (Vector3.Distance(transform.position, player.position) < followDistance)
     canMove = false;
 else
     canMove = true; 
 }
     

b) Another solution which may be more sensible if your enemies do melee attack is to use OnCollisionEnter() and OnCollisionExit() and set in a boolean whether the enemy can or can't make additional movement towards the player.

c) Use Trigger colliders on your enemies. You can combine this with solution b) and use OnTriggerEnter() and OnTriggerExit() instead.

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 Great Alexander · Jan 08, 2013 at 08:07 PM 0
Share

Thanks so much buddy , this really helped me and it works so well I would to tell you more than thank you if there is something better to say :D

avatar image
0

Answer by Great Alexander · Jan 08, 2013 at 11:12 AM

Thanks buddy, but for the first solution there is a problem which is the followDistance variable can notbe substracted from transform.position because it is a vector and the other is just an integer

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 AlucardJay · Jan 08, 2013 at 11:12 AM 0
Share

Hi There.

Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.

You can convert this answer to a comment (or just edit your original question), you'll also get a better chance of getting an actual answer if the main list shows none or one answer in blue =]

Under the answer where it says edit | delete | more , click on more , then convert to comment

avatar image
0

Answer by Great Alexander · Jan 08, 2013 at 11:15 AM

There is another problem when I make followDistance a Vector3

When the scene start the enemy keeps goin away from the player

Comment
Add comment · Show 2 · 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 AlucardJay · Jan 08, 2013 at 11:15 AM 0
Share

Hi There.

Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.

You can convert this answer to a comment (or just edit your original question), you'll also get a better chance of getting an actual answer if the main list shows none or one answer in blue =]

Under the answer where it says edit | delete | more , click on more , then convert to comment

(also you don't have to wait for a moderator to approve a comment).

Please be patient if your question/reply doesn't show straight away.

As a new user, your posts and questions are held in a moderator que until it is approved and then it is displayed. When your karma rises, you'll be able to post questions, comments and answers without waiting for someone to approve it =]

Also : you should edit your question if the situation changes or new information arises, so new readers can find it quickly without reading all the comments.

Wow, you got alot of my generic posts there !

avatar image $$anonymous$$ · Jan 08, 2013 at 11:31 AM 0
Share

Yeah well I wrote "something like this" in my answer because I just wanted to show you a sketch of a possible solution. Of course you need to check if the enemy's too close and then do something. $$anonymous$$ake followDistance a float, and check if Vector3.Distance(transform.position, player.position) < followDistance and if so, modify the position or disable the movement.

EDIT: I rewrote the code in my original answer, this should work for you, solution a).

Sorry if it was unclear, but usually if I answer, I try to give a thought process rather than exact code, if it's not requested.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Basic AI Locked Axis 1 Answer

my ai wiggles slightly 1 Answer

Random movment, like mobs in wow 1 Answer

Enemy AI don't collide with objects or rotate. 0 Answers

object pauses at the each way points for a vary short time 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