Skip to content

Instantly share code, notes, and snippets.

@IRobS
Last active August 27, 2019 14:25
Show Gist options
  • Save IRobS/03bb33f2499c67f7cb7091479d1a0bfc to your computer and use it in GitHub Desktop.
Save IRobS/03bb33f2499c67f7cb7091479d1a0bfc to your computer and use it in GitHub Desktop.
playerMovement - mouse driven move that depends on the pathNode script
/**
* playerMovement - mouse driven move that depends on the pathNode script
*/
background: #4cb;
min-height: 100%;
<pre>
<code>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour
{
public Vector3 targetPosition;
pathNode localNode;
pathNode destinationNode;
pathNode finalDestinationNode;
bool inTransit = false;
private void Start()
{
//starting off moving to the editor-set start location
targetPosition.z = 0;
finalDestinationNode = FindClosestNode(targetPosition);
localNode = FindClosestNode(transform.position);
inTransit = true;
}
// Update is called once per frame
void Update()
{
//if we're already moving (aka inTransit) then we can't accept new input
if (inTransit == false)
{
//we can accept new input
//get the mouse-click location
if (Input.GetKeyDown(KeyCode.Mouse0))
{
//they clicked, we're in motion
targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
targetPosition.z = 0;
finalDestinationNode = FindClosestNode(targetPosition);
localNode = FindClosestNode(transform.position);
inTransit = true;
}
}
else
{
// We're moving!!
// Get our next destination
//(this probably doesn't need to be done every frame, but it handles the final destination being changed unexpectedly)
destinationNode = localNode.GetNextStepToDestination(finalDestinationNode);
//do the move towards the destinationNode
transform.position = Vector3.MoveTowards(transform.position, destinationNode.transform.position, Time.deltaTime * 5);
//check if we've reached the destination
if ((transform.position - destinationNode.transform.position).magnitude &lt 0.1f)
{
//we're here, update to the next node
localNode = destinationNode;
destinationNode = localNode.GetNextStepToDestination(finalDestinationNode);
}
}
if ((transform.position - finalDestinationNode.transform.position).magnitude &lt 0.1f)
{
//We've arrived at our destination: ready to stop moving and take more input
inTransit = false;
}
}
/******
* useful method to find the nearest pathNode to a specified location
* used after we click the mouse, or any other location is specified
* ***/
private pathNode FindClosestNode(Vector3 target)
{
pathNode[] allNodes = FindObjectsOfType(typeof(pathNode)) as pathNode[];
pathNode closestNode = allNodes[0];
float closestDistance = (allNodes[0].gameObject.transform.position - target).magnitude;
foreach (pathNode aNode in allNodes)
{
float distance = (aNode.gameObject.transform.position - target).magnitude;
if (closestDistance &gt distance)
{
closestDistance = distance;
closestNode = aNode;
}
}
return closestNode;
}
}</code>
</pre>
// alert('Hello world!');
{"view":"split","fontsize":"100","seethrough":"","prefixfree":"1","page":"result"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment