c# Vectors and Lerp

I need some help with my bad guy class. I’m trying to learn a simple vector movement. Pick a vector from bad guy to player, and move halfway there. Then another point will be picked, and it will move there. I’ll make something else happen when it gets close.

This is all just to learn stuff. Not sure what’s going on. My vector x and y isn’t getting cut in half, and the bad guy only moves once.

using UnityEngine;
using System.Collections;

public class Bad : MonoBehaviour { 
	public Transform player;
	private bool playerDetected;
	
	private Vector3 destinationPoint; 
	private bool moveInitiated;
	private bool moveReached;
	private float distanceFromPlayer;
	// player rotation
	
	private Vector3 zAxis = new Vector3(0, 0, 1);
	
	// Use this for initialization
	void Start () {

		playerDetected = false;
		moveInitiated = false;
		moveReached = false;
		
	}
	
	// Update is called once per frame
	void Update () {

		if (playerDetected) {
			
			transform.RotateAround(player.transform.position, zAxis, 0.5f);  

			
		} else {

			if(!moveInitiated && !playerDetected)
			{
				

				set_destination();
				moveInitiated = true;
				
			}
			
			if(moveInitiated && !playerDetected)
			{

				if(!moveReached)
				{
			
					move();

				}

				if(return_lerp_distance() <= 0.04)
				{
					
					moveInitiated = false;
					moveReached = false;
					Debug.Log("Move Complete");
					
				}
				
				
			}

		
		}

		// see if player is close enough to detect to go into player detected logic

		if(get_distance() <= 1.0f)
		{
			
			playerDetected = true;

		}else{
			
			playerDetected = false;
			
		}
		
		
		// end update
	}

	void set_destination()
	{
	
		Vector3 destinationPoint = player.transform.position - transform.position; 
		destinationPoint.x = destinationPoint.x * 0.5f;
		destinationPoint.y = destinationPoint.y * 0.5f;

	}

	void move()
	{

			
			transform.position = Vector3.Lerp(transform.position,destinationPoint, Time.deltaTime / 3.0f);

	}

	float get_distance()
	{

		distanceFromPlayer = Vector3.Distance (player.transform.position, transform.position);
		return distanceFromPlayer;

	}

	float return_lerp_distance()
	{

		return Vector3.Distance (transform.position, destinationPoint);

	}

}

Hi @Jabes22,

You seem to have a couple of logical mistakes here.
First and foremost (showing original code):

     void set_destination()
     {
     
         Vector3 destinationPoint = player.transform.position - transform.position; 
         destinationPoint.x = destinationPoint.x * 0.5f;
         destinationPoint.y = destinationPoint.y * 0.5f;
 
     }

You’re setting Vector3 destinationPoint =, a completely new variable, instead of using destinationPoint =, which would set the variable you use elsewhere.

On top of that, you’re not using your current position. You’ve only calculated the delta times 0.5f.
You would add transform.position to that to have it be relative to the baddy.

But there’s an easier way.

    void set_destination()
    {
        // From => To => 0.5f is halfway.
        destinationPoint = Vector3.Lerp( transform.position, player.position, 0.5f );
    }

Then there’s the issue of movement, to achieve a constant speed, you probably want to use this instead (MoveTowards):

    void move()
    {
        transform.position = Vector3.MoveTowards( transform.position, destinationPoint, speed * Time.deltaTime );
    }

With those changes the code works (tested it for you :wink: )

Extra note, since the player is a transform already, you don’t have to use player.transform.position, you can just use player.position.

Here is the adjusted code (removed the whitespace for clarity, you can add it back if you want):

using UnityEngine;
using System.Collections;

public class Bad : MonoBehaviour
{
    public Transform player;
    public float speed = 2f;
    private bool playerDetected;

    private Vector3 destinationPoint;
    private bool moveInitiated;
    private bool moveReached;
    private float distanceFromPlayer;
    // player rotation

    private Vector3 zAxis = new Vector3( 0, 0, 1 );

    // Use this for initialization
    void Start()
    {

        playerDetected = false;
        moveInitiated = false;
        moveReached = false;

    }

    // Update is called once per frame
    void Update()
    {
        if( playerDetected )
        {
            transform.RotateAround( player.position, zAxis, 0.5f );
        }
        else
        {
            if( !moveInitiated && !playerDetected )
            {
                set_destination();
                moveInitiated = true;
            }

            if( moveInitiated && !playerDetected )
            {
                if( !moveReached )
                {
                    move();
                }

                if( return_lerp_distance() <= 0.04f )
                {
                    moveInitiated = false;
                    moveReached = false;
                    Debug.Log( "Move Complete" );
                }
            }
        }

        // see if player is close enough to detect to go into player detected logic
        if( get_distance() <= 1.0f )
        {
            playerDetected = true;
        }
        else
        {
            playerDetected = false;
        }

        // end update
    }

    void set_destination()
    {
        // From => To => 0.5f is halfway.
        destinationPoint = Vector3.Lerp( transform.position, player.position, 0.5f );
    }

    void move()
    {
        transform.position = Vector3.MoveTowards( transform.position, destinationPoint, speed * Time.deltaTime );
    }

    float get_distance()
    {
        distanceFromPlayer = Vector3.Distance( player.position, transform.position );
        return distanceFromPlayer;
    }

    float return_lerp_distance()
    {
        return Vector3.Distance( transform.position, destinationPoint );
    }
}

Lastly, you could use the following if you wanted to move to the player:

        void move()
        {
            transform.position = Vector3.MoveTowards( transform.position, player.position, speed * Time.deltaTime );
        }

I hope that helps!
If it does, please accept this answer, it’d be much appreciated!

If you need any more details or advice, let me know!

Best of luck!

Cheers,

ThePersister