Last active
August 27, 2019 14:25
-
-
Save IRobS/03bb33f2499c67f7cb7091479d1a0bfc to your computer and use it in GitHub Desktop.
playerMovement - mouse driven move that depends on the pathNode script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* playerMovement - mouse driven move that depends on the pathNode script | |
*/ | |
background: #4cb; | |
min-height: 100%; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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 < 0.1f) | |
{ | |
//we're here, update to the next node | |
localNode = destinationNode; | |
destinationNode = localNode.GetNextStepToDestination(finalDestinationNode); | |
} | |
} | |
if ((transform.position - finalDestinationNode.transform.position).magnitude < 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 > distance) | |
{ | |
closestDistance = distance; | |
closestNode = aNode; | |
} | |
} | |
return closestNode; | |
} | |
}</code> | |
</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// alert('Hello world!'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"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