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 /
  • Help Room /
This question was closed Nov 23, 2017 at 08:07 AM by NEGATIVERAGDOLL for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by NEGATIVERAGDOLL · Oct 25, 2017 at 06:30 AM · ai problems

Stop before player

Hello, I am making a script for an enemy to follow you and I am wondering how would I add it in so the enemy stops lets say 1 before you and not just continue to circle me? Thank you in advanced!

Here is my script:

  var target : Transform;
  var moveSpeed = 20;
  var rotationSpeed = 5;
  var myTransform : Transform;
  private var realRotation : Quaternion;
  
  function Awake() {
      myTransform = transform;
      realRotation = myTransform.rotation;
  }
  
  function Start() {
      target = GameObject.FindWithTag("Player").transform;
  }
  
  function Update () {
  
      myTransform.rotation = realRotation; // Resume it
  
      myTransform.rotation = Quaternion.Slerp(
          myTransform.rotation,
          Quaternion.LookRotation(target.position - myTransform.position),
          rotationSpeed * Time.deltaTime);
  
      myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
  
      realRotation = myTransform.rotation;
      myTransform.rotation = Quaternion.Euler(0, myTransform.eulerAngles.y, 0); // Fake it
  }

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

  • Sort: 
avatar image
0
Best Answer

Answer by Cuttlas-U · Oct 25, 2017 at 11:32 AM

hi; u just need an if statement to check the distance;

 if ( Vector3.Distance(enemyTransform.pos , Player.Transform.pos ) > 10 )
 {
 
 //chase the player;
 
 }

so this code enemy will follow to player untile its 10 point away;

Comment
Add comment · Show 11 · 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 NEGATIVERAGDOLL · Oct 25, 2017 at 12:23 PM 0
Share

Thanks for the reply mate!

Problem is though when I add it in the script it doesn't work. Is there any chance you could put it into the script as I might be doing something wrong? Thanks in advanced!

avatar image Cuttlas-U NEGATIVERAGDOLL · Oct 25, 2017 at 03:42 PM 0
Share

hi again ; it should work ! maybe the value 10 is too big and its already in range ; so show me your full script to check and your self go change the value and test again;

avatar image NEGATIVERAGDOLL Cuttlas-U · Oct 26, 2017 at 05:58 AM 0
Share

It wasn't that. When I would put the script in it would say "$$anonymous$$ Identifier Player" and the same for "enemy"

Show more comments

Follow this Question

Answers Answers and Comments

119 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

How to use NavMeshAgent on unity 5 car 0 Answers

How can an Animator FSM get public variables?,Making a Variable Holder for an FSM 0 Answers

how to search for a random point until the condition is true? 0 Answers

COMPILER Error 0 Answers

How do I make an AI(cube) that follows and rams the player? 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