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 /
avatar image
0
Question by onic2999 · Apr 25, 2016 at 04:00 PM · enemyai

Making enemy follow player

So I have been trying to make my object(a sphere with sphere collider and sphere collider trigger) follow my player,but the script is giving out errors and i have no idea what do they mean,and how to fix them,here is the piece of code I wrote:

     // Use this for initialization
     void Start () {
         
 
     }
     
     // Update is called once per frame
     void Update () {
         if (playerdestructor == true)
             Debug.Log ("touchy");
         rigid.AddForce (Vector2.MoveTowards (self, player, 10f));
         if (playerdestructor == false)
             Debug.Log ("lonely :(");
     }
     void OnTriggerEnter2D (Collider2D col){
         if (col.tag == "Player")
             playerdestructor = true;
     }
     void OnTriggerExit2D (Collider2D coly){
         if (coly.tag == "Player")
             playerdestructor = false;
     }
 }
Comment
Add comment · Show 4
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 Jessespike · Apr 25, 2016 at 05:17 PM 0
Share

What and where are the errors?

avatar image onic2999 · Apr 26, 2016 at 07:53 AM 0
Share

my object jumps directly to my player even if i set a speed it teleports to my play and won't follow.I have tweaked it to work on my project,this is what i have got so far

 void Update () {
         transform.position = new Vector3 (xaxis, 0); 
         if (playerdestructor == true) {
             Debug.Log ("touchy");
             rigid.transform.position = Vector3.$$anonymous$$oveTowards (self.position, player.position, 5) * speed;
         }




avatar image Jessespike · Apr 26, 2016 at 05:39 PM 0
Share

The max$$anonymous$$oveDeltas are quite high, move the speed variable and try a smaller value like 0.1

  rigid.transform.position = Vector3.$$anonymous$$oveTowards (self.position, player.position, speed);
avatar image onic2999 · Apr 27, 2016 at 08:08 AM 0
Share

well thanks for the advice,it doesn't follow my player but it doesn't teleport to it instantly either,Any fix?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Winterblood · Apr 27, 2016 at 01:59 PM

You're moving "self" towards "player" with a distance cap of "speed". This will happen instantly. If you want to spread the movement over several frames, multiply speed by Time.deltaTime:

 rigid.transform.position = Vector3.MoveTowards (self.position, player.position, speed * Time.deltaTime);

The delta time is the time between frames, ie. how much time has passed since the last Update.

This will also make the movement framerate-independent (a slow machine will have a larger deltaTime, so the object will move in larger steps but take the same time to arrive as it would on a fast machine).

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

55 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

Related Questions

(14,40): UCE0001: ';' expected. Insert a semicolon at the end. HELP 0 Answers

i'm trying to make an enemy attack the player when the player is within range but it says an object reference cannot be found but i'm not sure where the issue is 0 Answers

2D Boomerang Style Weapon 1 Answer

enemy into the air an blow up 0 Answers

How do I stop enemy AI from bumping and entering into my player? 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