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 DAzorn · Feb 26, 2014 at 10:28 PM · 2dmovementplayertargetenemy ai

Enemy AI Movement To A Target(Player) In A 2D Game

Hello everybody, this is my C# Script:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAI : MonoBehaviour {
 
     public Transform Target;
     private GameObject Enemy;
     private GameObject Player;
     private float Range;
     public float Speed;
 
 
     // Use this for initialization
     void Start () {
         Enemy = GameObject.FindGameObjectWithTag ("Enemy");
         Player = GameObject.FindGameObjectWithTag ("Player");
     }
     
     // Update is called once per frame
     void Update () {
         Range = Vector2.Distance (Enemy.transform.position, Player.transform.position);
         if (Range <= 15f) {
             transform.Translate(Vector2.MoveTowards (Enemy.transform.position, Player.transform.position, Range) * Speed * Time.deltaTime);
                 }
     }
 }

Now, this script work for 3 seconds (in this time i move to two different directions whit the player) , but when i try to move in a third different direction the Enemy still goes in the previous direction! Is that a script problem? Help me please! [I'm italian, so dont speak strong english... use easy words :) ]

Comment
Add comment · Show 2
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 robertbu · Feb 26, 2014 at 10:34 PM 0
Share

Likely your issues are with line 24. I'm not sure of the fix since I don't know your setup. You have to game objects, an enemy and a player, plus you have whatever this script is attached to. How is this object related to the other two, and what do you want to happen?

avatar image DAzorn · Feb 27, 2014 at 09:08 AM 0
Share

I want the game object enemy follow the player

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Domo23000 · Sep 18, 2014 at 11:02 PM

use this

 Vector2 velocity = new Vector2((transform.position.x - player.position.x) * speed, (transform.position.y - player.position.y) * speed);
         rigidbody2D.velocity = -velocity;

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 CORYB · Oct 02, 2016 at 06:35 PM 0
Share
 Vector2 velocity = new Vector2((transform.position.x - Player.transform.position.x) * Speed, (transform.position.y - Player.transform.position.y) * Speed);
             GetComponent<Rigidbody2D>().velocity = -velocity;

avatar image carlosbryanbalbastro CORYB · Nov 08, 2019 at 01:08 PM 0
Share

@CORBY hmm..... where shoul you put it?

Vector2 velocity = new Vector2((transform.position.x - Player.transform.position.x) Speed, (transform.position.y - Player.transform.position.y) Speed); GetComponent().velocity = -velocity;

avatar image
0

Answer by F-N · Feb 27, 2014 at 09:29 AM

Wy dont you make the enemy look at the player and then call: transform.position += transform.forward speed Time.deltaTime

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 DAzorn · Feb 27, 2014 at 10:00 AM 0
Share

Because that is a 2d game guy

avatar image DAzorn · Feb 27, 2014 at 10:10 AM 0
Share

Cause is a 2D game

avatar image
0

Answer by AmarokStudios · Jul 22, 2014 at 12:28 PM

Can somebody translate this to Javascript? I'm not very good with C# and I'd like to understand this script better. I don't like copying and pasting scripts without knowing how to make adjustments to them. Thanks in advance!

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 $$anonymous$$ · Jul 30, 2014 at 06:48 PM 1
Share
 #pragma strict
 
 ///Variables Declaration///
 var target : Transform;
 private var enemy : GameObject;
 private var player : GameObject;
 private var range : float;
 var speed : float;
 
 
 ///Inizialization///
 function Start ()
 {
     enemy = GameObject.FindGameObjectsWithTag("Enemy");
     player = GameObject.FindGameObjectsWithTag("Player");
 }
 
 
 ///Fixed Update///
 function FixedUpdate ()
 {
     //Vector2.Distance = enemy.transform.position - player.transform.position
     range = Vector2.Distance(enemy.transform.position, player.transform.position);
     if(range <= 15f)
     {
         transform.Translate(Vector2.$$anonymous$$oveTowards(enemy.transform.position, player.transform.position, range) * speed * Time.deltaTime);
     }
 }



Translated but not checked!

Hope this will help you anyway!

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

27 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

Related Questions

Player movement boudaries in 2D 1 Answer

Making my 2d character's arm follow the mouse, scrpit not working, please help :c 1 Answer

Player stops moving upon collision 0 Answers

How can I move the character like 2D games? 1 Answer

How to limit the forces applied to an object to just forward, left, and right? 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