Skip to content

Instantly share code, notes, and snippets.

@Megumi3
Created May 26, 2015 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Megumi3/7723ba2e36f77ebd3483 to your computer and use it in GitHub Desktop.
Save Megumi3/7723ba2e36f77ebd3483 to your computer and use it in GitHub Desktop.
Unity Navigation Test
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
NavMeshAgent agent;
RaycastHit hit;
Ray ray;
Animator animator;
// Use this for initialization
void Start () {
agent = GetComponent<NavMeshAgent>();
animator = GetComponent<Animator> ();
animator.SetBool ("Run", false);
}
// Update is called once per frame
void Update () {
// 左クリックしたときに、
if (Input.GetMouseButtonDown (0)) {
// マウスの位置からRayを発射して、
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
// 物体にあたったら、
if (Physics.Raycast (ray, out hit, 100f)) {
// その場所に、Nav Mesh Agentをアタッチしたオブジェクトを移動させる
agent.SetDestination (hit.point);
// "Run"アニメーションに遷移
animator.SetBool ("Run", true);
}
}
// 目的地とプレイヤーとの距離が1以下になったら、
if (Vector3.Distance(hit.point, transform.position) < 1.0f) {
// "Run"アニメーションから抜け出す
animator.SetBool ("Run", false);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment