Skip to content

Instantly share code, notes, and snippets.

@smallrice45
Created June 18, 2014 14:04
Show Gist options
  • Save smallrice45/05c3f38d7c5c1699b13a to your computer and use it in GitHub Desktop.
Save smallrice45/05c3f38d7c5c1699b13a to your computer and use it in GitHub Desktop.
練習Navmesh
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;
}
}
}
}
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