Advertisement

Unity Mid Point

Started by December 04, 2016 11:48 AM
4 comments, last by Andor Patho 7 years, 11 months ago

Hello,

I'm gettin gy hands dirty to learn a bit of math, and Unity again. I'm trying to move the gameObject transform to a mid point from player.transform to transform.location.

It's just some dummy ai, so nothing major going on.

Right now the baddy, which is gameObject doesn't move anywhere in the Vector3.Lerp.

Thanks for help.


using UnityEngine;
using System.Collections;

public class bad : MonoBehaviour { 
	private Vector2 speed;
	public Transform player;
	private bool playerDetected;

	private Vector3 destinationVector; 
	private Vector3 destinationPoint; 
	private bool moveComplete;

	// player rotation

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

	// Use this for initialization
	void Start () {

		speed.x = 1.0f;
		speed.y = 1.0f;
		playerDetected = false;

		moveComplete = false;
	
	}
	
	// Update is called once per frame
	void Update () {

		if (!moveComplete) {

			Vector3 destinationVector = player.transform.position - transform.position; 
			destinationPoint = new Vector3(destinationVector.x + transform.position.x / 2.0f, destinationVector.y - transform.position.y / 2.0f, 0.0f);
		
		}
	
	
		if (playerDetected) {

		//	transform.RotateAround (player.transform.position, zAxis, 0.5f); 
		    
		} else {

			if(!moveComplete)
			{
		
				Vector3.Lerp(transform.position,destinationPoint, 1.0f * Time.deltaTime);
		
				if(Vector3.Distance(transform.position, destinationPoint) <= 8)
				{

					moveComplete = true;

				}

			}

		}

		if(Vector3.Distance(player.transform.position, transform.position) <= 8)
		{

			playerDetected = true;

		}else{

			playerDetected = false;

		}

	}
}

Hi,

your immediate issue is this line:


Vector3.Lerp(transform.position,destinationPoint, 1.0f * Time.deltaTime);

Lerp returns the interpolated value, so if you want the transform position to change, you should do this:


transform.position = Vector3.Lerp(transform.position,destinationPoint, 1.0f * Time.deltaTime);

The way you calculate the destinationPoint is also weird, but I'm sure you can figure it out with a little experimentation.

Advertisement

Yeah, that's the main gist. But while you're at it, you're checking for !moveComplete twice, you could move that all under the second check. That and your destinationPoint calculation is a little strange, what is it attempting to do, pick a point half way to the player? Might as well call Vector3.Lerp with a value of 0.5f.

Also, you can do 'else if' all in one line for less verbose code.

The reason I'm trying to make the move seperate from lerp initialization is because I don't want the bad guy to always move towards the halfway point of the player. I'm going to create slightly different logic for when the player is in the vicinity, or bad guy detects him.

There's a problem with Unity or the code now. For some reason the script doesn't run. I've got break points to analyze execution, but not even start() seems to be called. I double checked, the script is still attached.

I fixed the mid point calculation too. I should have thoughr of multiplication by a scalar. Picked up that trick from Jorge Rodriguez on YouTube video.


using UnityEngine;
using System.Collections;

public class bad : MonoBehaviour { 
	private Vector2 speed;
	public Transform player;
	private bool playerDetected;

	private Vector3 destinationVector; 
	private Vector3 destinationPoint; 
	private bool moveComplete;
	private bool moveInitiated;
	// player rotation

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

	// Use this for initialization
	void Start () {

		speed.x = 1.0f;
		speed.y = 1.0f;
		playerDetected = false;

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

		if (playerDetected) {

			//	transform.RotateAround (player.transform.position, zAxis, 0.5f); 
		    
		} else {

			if(!moveInitiated)
			{

				Vector3 destinationVector = player.transform.position - transform.position; 
                                destinationPoint = new Vector3(destinationVector.x * 0.5f, destinationVector.y * 0.5f, 0.0f);
				moveInitiated = true;
						
				if(!moveComplete && moveInitiated)
				   {
						transform.position = Vector3.Lerp(transform.position,destinationPoint, 1.0f * Time.deltaTime);

					if(Vector3.Distance(transform.position, destinationPoint) <= 8)
					{
						
						moveComplete = true;
						moveInitiated = false;

					}


				}

			}

		}

			// see if player is close enough to detect to go into player detected logic
		if(Vector3.Distance(player.transform.position, transform.position) <= 8)
		{

			playerDetected = true;

		}else{

			playerDetected = false;

		}

	}
}

I've cleaned the code here a lot more, but it's still not working right. The rotation part is okay on playerDetected, but there's still some problems with the lerp. I'm not sure why my y and x positions aren't getting cut in half. Also the bad guy only moves once. It never tries to plot a new lerp.


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(Vector3.Distance(transform.position, destinationPoint) <= 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;

	}

}

You're still using Vector3.Lerp incorrectly. The third parameter to Lerp is supposed to be a float in the range [0,1] that acts as a parameter to the interpolation. If you pass the same value to it, you will get the same result. Vector3.MoveTowards would be more suitable for what you are trying to do here.

As for the destinationPoint not being correct, you are creating a new Vector in the set_destination method instead of modifying the private variable (the compiler should generate a warning at least). You are also multiplying by 0.5 at the wrong time. You probably want this:


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

which can also be written more succintly as:


void set_destination()
{
    destinationPoint = (transform.position + player.transform.position) * 0.5f;
}

This topic is closed to new replies.

Advertisement