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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Enemy Teleporting not MovingTowards

Discussion in 'Scripting' started by CodeMateo, Jun 21, 2020.

  1. CodeMateo

    CodeMateo

    Joined:
    May 21, 2020
    Posts:
    77
    Hey guys, having some trouble. I have a Vector2.MoveTowards, and it seems not to work. It seems to be moving the sprite immediately to the new spot with no walking or moving. Any help would be great!
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyMovement : MonoBehaviour
    6. {
    7.     private float x;
    8.     private float y;
    9.     public float minX;
    10.     public float maxX;
    11.     public float minY;
    12.     public float maxY;
    13.  
    14.     public Animator animator;
    15.  
    16.     public float enemySpeed = 5f;
    17.  
    18.     public bool move;
    19.     public bool patrol;
    20.  
    21.     private float waitTime;
    22.     public float startWaitTime;
    23.  
    24.     public Transform EnemyTransform;
    25.     public Transform StartTransform;
    26.  
    27.     Vector3 startPos;
    28.     Vector2 characterScale;
    29.     float characterScaleX;
    30.     Vector2 movement;
    31.  
    32.     void Start()
    33.     {
    34.         characterScale = transform.localScale;
    35.         characterScaleX = characterScale.x;
    36.         animator = GetComponent<Animator>();
    37.  
    38.         waitTime = startWaitTime;
    39.  
    40.         //StartTransform.position = this.transform.position;
    41.         startPos = this.EnemyTransform.position;
    42.         move = true;
    43.     }
    44.  
    45.     void Update()
    46.     {
    47.         animator.SetBool("Patrol", patrol);
    48.  
    49.         //transform.position = Vector2.MoveTowards(StartTransform.position, EnemyTransform.position, moveSpeed * Time.deltaTime);
    50.         //transform.position = Vector2.MoveTowards(transform.position, EnemyTransform.position, enemySpeed * Time.deltaTime);
    51.  
    52.         if (move == true)
    53.         {
    54.             Patrol();
    55.         }
    56.  
    57.         if (Vector2.Distance(transform.position, EnemyTransform.position) < 0.2f)
    58.              {
    59.                if (waitTime <= 0)
    60.                {
    61.                 waitTime = startWaitTime;
    62.                 move = true;
    63.                 UnityEngine.Debug.Log("Idle done");
    64.             }
    65.               else
    66.               {
    67.                 waitTime -= Time.deltaTime;
    68.                 patrol = false;
    69.               }
    70.             }
    71.  
    72.  
    73.  
    74.  
    75.         movement.x = Input.GetAxisRaw("Horizontal");
    76.         movement.y = Input.GetAxisRaw("Vertical");
    77.  
    78.         if (Input.GetAxis("Horizontal") < 0)
    79.         {
    80.             characterScale.x = -characterScaleX;
    81.         }
    82.         if (Input.GetAxis("Horizontal") > 0)
    83.         {
    84.             characterScale.x = characterScaleX;
    85.         }
    86.     }
    87.  
    88.     void FixedUpdate()
    89.     {
    90.         transform.position = Vector2.MoveTowards(transform.position, EnemyTransform.position, enemySpeed * Time.fixedDeltaTime);
    91.     }
    92.  
    93.  
    94.     void Patrol()
    95.     {
    96.         x = Random.Range(minX, maxX);
    97.         y = Random.Range(minY, maxY);
    98.         patrol = true;
    99.         //EnemyTransform.position = StartTransform.position + new Vector3(x, y);
    100.         EnemyTransform.position = startPos + new Vector3(x, y);
    101.         move = false;
    102.         UnityEngine.Debug.Log("It's working");
    103.     }
    104. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,814
    Sprinkle some more Debug.Log() statements in to find what code is running, and why that code is running.

    Is it perhaps your enemySpeed is really big? Is it perhaps your
    move
    boolean is not what you expect? etc. etc.
     
    CodeMateo likes this.
  3. CodeMateo

    CodeMateo

    Joined:
    May 21, 2020
    Posts:
    77
    Oh my programming works perfectly. The enemy patrols the area, and sits idle for a little bit and goes back to patrolling. My problem is that instead of walking to the next point, he just immediately teleport there and my knowledge of C# is not enough to understand why that's so. Do I need to put a rigid body 2D in my script?
     
  4. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    This is an interesting script. It says it's for the EmemyMovement. yet in some places is checks transform.position and EnemyPosion. and it has player input checks.
    which tells me that script is attached to the player

    line 100 is the only place that I can see that moves the enemy. and this line would teleport the enemy to a random position.
    EnemyTransform.position = startPos + new Vector3(x, y);

    so, you need to convert this into a MoveTowards() just like you did for the player.

    and even better would be to create a script for the enemy and attach it to the enemy. but like you said. you're new and still learning.
     
    CodeMateo and Yoreki like this.
  5. CodeMateo

    CodeMateo

    Joined:
    May 21, 2020
    Posts:
    77
    Thank you. My script is destined for a enemy prefab that is patrolling. He then spawns in a small area randomly so I need him to walk in a -5 by 5 square. The script moves perfectly between his little patrol and an idle animation, the problem is he’s just teleporting to the next spot not walking towards it that’s all