Created
June 18, 2014 14:04
-
-
Save smallrice45/05c3f38d7c5c1699b13a to your computer and use it in GitHub Desktop.
練習Navmesh
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
using UnityEngine; | |
using System.Collections; | |
public class enemyMovement : MonoBehaviour { | |
NavMeshAgent agent; | |
void Start () { | |
agent = GetComponent< NavMeshAgent >(); | |
} | |
void Update () { | |
if (Input.GetMouseButtonDown(1)) { | |
// ScreenPointToRay() takes a location on the screen | |
// and returns a ray perpendicular to the viewport | |
// starting from that location | |
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); | |
RaycastHit hit; | |
// Note that "11" represents the number of the "ground" | |
// layer in my project. It might be different in yours! | |
LayerMask mask = 1 << 11; | |
// Cast the ray and look for a collision | |
if (Physics.Raycast(ray, out hit, 200, mask)) { | |
// If we detect a collision with the ground, | |
// tell the agent to move to that location | |
agent.destination = hit.point; | |
} | |
} | |
} | |
} |
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
using UnityEngine; | |
using System.Collections; | |
public class playerMovement : MonoBehaviour { | |
NavMeshAgent agent; | |
void Start () { | |
agent = GetComponent< NavMeshAgent >(); | |
} | |
void Update () { | |
if (Input.GetMouseButtonDown(0)) { | |
// ScreenPointToRay() takes a location on the screen | |
// and returns a ray perpendicular to the viewport | |
// starting from that location | |
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); | |
RaycastHit hit; | |
// Note that "11" represents the number of the "ground" | |
// layer in my project. It might be different in yours! | |
LayerMask mask = 1 << 11; | |
// Cast the ray and look for a collision | |
if (Physics.Raycast(ray, out hit, 200, mask)) { | |
// If we detect a collision with the ground, | |
// tell the agent to move to that location | |
agent.destination = hit.point; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment