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 /
This question was closed Apr 19, 2017 at 11:59 AM by Pennywise881 for the following reason:

Other

avatar image
1
Question by Pennywise881 · Apr 19, 2017 at 01:24 PM · unity 2dtransform.positionvectorsvector3.movetowards

Vector2.MoveTowards problem

Hi there, I am having some problems with moving an enemy towards the player when it detects that the player is near it. The code is pretty straight forward. The Update() function checks if the player is close enough to the enemy, and when it is, the boolean variable is set to true, and when it is true its supposed to move the enemy towards the player. Here is the code:

 using UnityEngine;
 
 public class Slime_Attack : MonoBehaviour
 {
     public bool attack;
     GameObject player;
     float moveSpeed;
 void Start()
 {
     moveSpeed = 3;
     player = GameObject.FindGameObjectWithTag("Player");
     // attack = false;
 }

 void Update()
 {
     checkForPlayer();
     if (attack)
     {
         transform.position = Vector2.MoveTowards(transform.position, player.transform.position, moveSpeed * Time.deltaTime);
     }
 }

 private void checkForPlayer()
 {
     if (Vector2.Distance(transform.position, player.transform.position) >= 5) attack = false;
     else if (Vector2.Distance(transform.position, player.transform.position) <= 3) attack = true;
 }
 }

The problem that I am facing is that this is kind of inconsistent. The enemy moves towards the player fine in the beginning and then it either slows down when its close to the player or in some rare cases it move really fast towards the player. It slows down especially when the player is on the left of the player for the most part, but then again in some cases, it also moves slowly when the player is to its right. I have read every documentation regarding Vector2.MoveTowards and have checked almost all other answers similar to this problem but I still cant get this to work. Any help is appreciated. Thanks

Comment
Add comment · Show 1
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 Pennywise881 · Apr 19, 2017 at 11:58 AM 1
Share

Alright guys I figured it out, basically what u have to do is, if the gameobject has a rigidbody component, then you need to use GetComponent<Rigidbody2D>().$$anonymous$$ovePosition(Vector2.$$anonymous$$oveTowards(transform.position, player.transform.position, moveSpeed * Time.deltaTime)); . This will fix the problem. Thanks :)

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Why cant any operations be performed in camera when loaded from the previous scene? 0 Answers

Rigidbody2D.Velocity = Vector2 not changing X Position 1 Answer

How to get position of instantiated object before it is destroyed 2 Answers

Get an Object to move to a Position for a Fixed time 2 Answers

How can i change position of my character 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