Moving through a waypoint system using a keypress.

Hi,

How would I go about moving through a set of waypoints using a keypress? I currently have it working but it is not very good at all. The character moves to the point and stops when it hits the trigger but if the collider on the waypoint or on the char is too large, it can require more than one press to get out of the collider. If its too small if will mess up and fly off the map.

The game I am making is for visually impaired people who have different levels of blindness and it uses text-to-speech to help guide them.

This is my script for waypoints and so on is this:

var waypoint : Transform[];
var speed : float = 20;
private var currentWaypoint : int;
var loop : boolean = true;
var isMoving : boolean = false;

var velocity : Vector3;

function Update () {

	if(Input.anyKeyDown && isMoving == false)
	{	
		isMoving = true;
	}
	
	if(currentWaypoint < waypoint.length){
		var target : Vector3 = waypoint[currentWaypoint].position;
		var moveDirection : Vector3 = target - transform.position;
		
	
	velocity = rigidbody.velocity;
		
	transform.LookAt(target);
	
	if(moveDirection.magnitude < 1)
	{
		if(isMoving != true)
		{
			currentWaypoint++;
		}
	}	
	
	if(isMoving == true)
	{
		animation.CrossFade("run");
		velocity = moveDirection.normalized * speed;
		rigidbody.velocity = velocity;
	}
  }
}

function OnTriggerStay(other : Collider)
{
	if(other.tag == "wayPoint")
	{
		animation.Play("idle1");
		rigidbody.velocity = Vector3(0,0,0);
		isMoving = false;
	}
}

Thanks!

Okay, nice one would be using Lerp instead of checking distance per frame. Lerp will definetly put your object exactly where you pointed him to be.

I give you a small snippet and I hope you will use it properly. If something happens just ask :slight_smile:

So you do this (where the _distance you calculate only once when you press the button):

    float t = 0f;
	Vector3 _startPosition = Vector3.zero;
	Vector3 _selectPosition = Vector3.zero;
	float _distance = 0f;
	void Update()
	{		
		if( isMoving )	
		{
			object.transform.position = Vector3.Lerp(_startPosition, _selectPosition, t+= 0.5f/_distance);
		
			if ( t >= 1f)
				Complete();	
		}
	}

Instead of using triggers, you could try using distance to check if your player has reached every waypoint.