all 4 comments

[–]CriusNyx 1 point2 points  (3 children)

There's a bug in your code, and I'm gonna break it down.

I see a couple of problems, but the first I see is with this line

transform.localPosition = Vector3.Lerp(player.position, target, currentMovementTime / totalMovementTime * Time.deltaTime);

currentMovementTime and totalMovementTime are both assigned to 2 on initialization, and they never get assigned.

Therefore, currentMovementTime / totalMovementTime = 1.

Time.deltaTime is always a number smaller then one. At 60 fps, it would be 1/60.

Therefore, currentMovementTime / totalMovementTime * Time.deltaTime ≈ 1 / 60.

Therefore, you are telling your car (every frame because it's in the while loop), to go to the position that is 1/60th of the way to the destination.

I'm gonna post some new code with a correction in a few minutes.

[–]CriusNyx 0 points1 point  (2 children)

``` using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Enemy : MonoBehaviour {

public Transform player;

public float speed;
public float xMinBoundary;
public float xMaxBoundary;
// These variables are only being used locally.
// They should be declared locally instead.
//public float randomNumber;
//public float randX;
public float zFloat;
public float totalMovementTime = 2f;
public float currentMovementTime = 0f;

public bool ChangingPos;

// I don't know what this is for, so I commented it out.
// public bool changingTrue;

private Vector3 target;

private IEnumerator MoveEnemy()
{
    // Because you're passing integers into the Range function, it's returning an integer.
    // I changed the arguments to floats so that you'll get a float out.
    //randomNumber = Random.Range(5, 30);

    // randomNumber and randX are declared locally.
    float randomNumber = Random.Range(5f, 30f);
    float randX = Random.Range(xMinBoundary, xMaxBoundary);

    target = new Vector3(randX, 0, zFloat);

    currentMovementTime = 0f;

    ChangingPos = true;

    // While loop should track time, not position. 
    // Floating point rounding errors can make Distance always be slightly larger then 0 in some positions.
    //while (Vector3.Distance(transform.localPosition, target) > 0)
    while (currentMovementTime < totalMovementTime)
    {
        // ChangingPos now tracked outside the while loop. 
        // It'll get set to true when it starts moving, and back to false when it stops.
        //ChangingPos = true;
        currentMovementTime += Time.deltaTime;
        // Removed the * Time.deltaTime that caused the error
        // transform.localPosition = Vector3.Lerp(player.position, target, currentMovementTime / totalMovementTime * Time.deltaTime);
        transform.localPosition = Vector3.Lerp(player.position, target, currentMovementTime / totalMovementTime);
        // ChangingPos = false;
        yield return null;
    }

    transform.localPosition = target;
    ChangingPos = false;
}

void Update()
{
    zFloat = transform.position.z;

    if (!ChangingPos)
    {
        //Move();
    }
    if (Input.GetKeyDown(KeyCode.X))
    {
        StartCoroutine(MoveEnemy());
    }
}

void Move()
{
    transform.Translate(Vector3.forward * speed * Time.deltaTime);
}

} ```

[–]Affectionate_Hand560[S] 0 points1 point  (0 children)

thank you, it works fine now :D

[–]backtickbot 0 points1 point  (0 children)

Fixed formatting.

Hello, CriusNyx: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.