Created
May 26, 2015 15:45
-
-
Save Megumi3/7723ba2e36f77ebd3483 to your computer and use it in GitHub Desktop.
Unity Navigation Test
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 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