Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

2D AI Movement using 'transform.position +=' C#

Discussion in 'Scripting' started by TomProg, Sep 9, 2016.

  1. TomProg

    TomProg

    Joined:
    Aug 21, 2016
    Posts:
    17
    I'm making a 2D TopDown AI system that moves the enemy to the player. The problem is that the enemy constantly moves to the bottom left corner.
    Here's my code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ZombieAI : MonoBehaviour {
    5.  
    6.     float targetDistanceX;
    7.     float targetDistanceY;
    8.     Vector3 velocity;
    9.     public Entity zombie;
    10.     public Entity player;
    11.     Rigidbody2D rb;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.  
    16.         rb = GetComponent<Rigidbody2D> ();
    17.  
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void FixedUpdate () {
    22.        
    23.         targetDistanceX = player.transform.position.x - transform.position.x;
    24.         targetDistanceY = player.transform.position.y - transform.position.y;
    25.  
    26.         velocity = new Vector3 (targetDistanceX , targetDistanceY, 0);
    27.  
    28.         transform.position -= (velocity.normalized * zombie.speed * Time.fixedDeltaTime);
    29.  
    30.  
    31.  
    32.     }
    33.  
    34. }
    If anyone knows the problem, feel free to reply ;)

    Kind regards,
    Tom
     
  2. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    You might want to use Vector2.MoveTowards as currently what you are doing is extracting the target position from your current position which will always move you towards 0.0 (which is the bottom left).
     
  3. TomProg

    TomProg

    Joined:
    Aug 21, 2016
    Posts:
    17
    I tried it, but now it doesn't move at all(if I push it, it does move btw).
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    and we're going to use our psychic powers to see the code you tried to use? :p

    you could just use "+=" instead of "-=" so you're applying the right direction... (which is what Timelog was referring to I think)



    just going to comment on this... AIs make decisions, they don't perform the concrete movement of anything to anywhere. If you make sure you break up how you think about what the scripts are doing you'll, by extension, build better script architecture.

    AIs take input and make decisions.
    Pathfinders work out a path from point a to b.
    Movement/Locomotion/whateverYouWantToCallIt scripts actually move the object.

    similarly
    Players make decisions
    InputController scripts handle the input
    Movement/Locomotion/whateverYouWantToCallIt scripts actually move the object. <--- it's the same line as above :) code reuse! :D
     
    Timelog likes this.