How To - Analog instead of digital movement

to get smooth movement you will need an update function and a new transform variable and float variable, you will also need to make a boolean function, and modify the the enumerator (note this isn’t a complete script only the modifications required);

private Transform toGoTo;
[SerializeField] float moveSpeed = 50f;

void Update(){
			transform.position = Vector3.MoveTowards(transform.position, toGoTo.position, moveSpeed * Time.deltaTime);

}

 IEnumerator FollowPath(List<Waypoint> wppath) {
	print ("Starting Patrol...");
	foreach (Waypoint waypoint in wppath) {
		toGoTo = waypoint.transform; //sets where the enemy should be going
		yield return new WaitUntil(() => isAtPosition(waypoint) == true); //waits until isatposition = true
	}
	ReachedEnd (); //ignore for later section
}

bool isAtPosition(Waypoint wp){
	if (this.transform.position == wp.transform.position) {
		return true;
	} else {
		return false;
	}
}
2 Likes

Hi Gavyn, I’m interested in how you did this and I tried to copy it, but with some problems.
Would you mind posting the entire script ?

Thanks !

As you requested here is the whole script StandUp_Gamer

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

public class EnemyMover : MonoBehaviour {

[SerializeField] ParticleSystem EndParticle;
[SerializeField] float moveSpeed;
[SerializeField]List <Waypoint> path;

private Transform toGoTo;

// Use this for initialization
void Start () {
//pathFinder is my waypoint calculator / breadths first search calculator
	pathFinder pathfinder = FindObjectOfType<pathFinder> ();
//path is the list of waypoints in pathfinder from start to finish and is retrieved with a function to avoid unwanted editing 
	path = pathfinder.GetPath ();
//starts the followpath routine and passes it the path list from pathfinder
	StartCoroutine (FollowPath (path));
}

void Update(){
	Move ();
}

//runs every frame for smooth movement (could put in Update but if I needed to expand script/ add more functions to the update I would have to do this anyways)
void Move(){
	float MovementSpeed = moveSpeed * Time.deltaTime;
	transform.position = Vector3.MoveTowards(transform.position, toGoTo.position, MovementSpeed);
}

IEnumerator FollowPath(List<Waypoint> wppath)
{
	//print ("Starting Patrol...");
	
foreach (Waypoint waypoint in wppath) {
		toGoTo = waypoint.transform;
		yield return new WaitUntil(() => isAtPosition(waypoint) == true);
	}
	ReachedEnd ();
}

//returns true when enemy has reached the position of the next waypoint
bool isAtPosition(Waypoint wp){
	if (this.transform.position == wp.transform.position) {
		return true;
	} else {
		return false;
	}
}

//things to do once object has reached end tile
void ReachedEnd()
{	
	var deathParticle = Instantiate (EndParticle, this.transform.position, Quaternion.identity, this.transform.parent.transform);
	deathParticle.Play ();
	Destroy (deathParticle.gameObject, deathParticle.main.duration);
	Destroy (this.gameObject);
}

//just a setter for scriptable objects
public void setSpeed(float speed){
	moveSpeed = speed;
}

}

If you need any more help just ask, there are some lines in there that are unneeded until further into the course, like when you start to add fx etc.

2 Likes

Privacy & Terms