Created
July 6, 2017 14:00
-
-
Save tsubaki/0e59b3d1ec6336dc9d02f4825102c8ba to your computer and use it in GitHub Desktop.
キャラクターを動かす
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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.AI; | |
public class Move : MonoBehaviour | |
{ | |
[SerializeField] float speed = 1; | |
static int idSpeed = Animator.StringToHash("Speed"); | |
Transform cameraTransform; | |
Animator animator; | |
NavMeshAgent agent; | |
void Start() | |
{ | |
cameraTransform = Camera.main.transform; | |
animator = GetComponent<Animator> (); | |
agent = GetComponent<NavMeshAgent> (); | |
} | |
void Update () | |
{ | |
var move = new Vector2 ( Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical")); | |
var cameraForward = Vector3.Scale(cameraTransform.forward, new Vector3(1, 0, 1)).normalized; | |
if (move != Vector2.zero) { | |
Vector3 direction = cameraForward * move.y + cameraTransform.right * move.x; | |
transform.localRotation = Quaternion.LookRotation (direction); | |
// キャラクターの位置を移動させる | |
agent.Move (direction * (Time.deltaTime * speed)); | |
} | |
animator.SetFloat (idSpeed, move.magnitude ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment