Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created July 6, 2017 14:00
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 tsubaki/0e59b3d1ec6336dc9d02f4825102c8ba to your computer and use it in GitHub Desktop.
Save tsubaki/0e59b3d1ec6336dc9d02f4825102c8ba to your computer and use it in GitHub Desktop.
キャラクターを動かす
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