Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active August 29, 2015 14:25
Show Gist options
  • Save tsubaki/c949a968201d8733e915 to your computer and use it in GitHub Desktop.
Save tsubaki/c949a968201d8733e915 to your computer and use it in GitHub Desktop.
キャラクターの移動(アニメーション・向き変更無し)
using UnityEngine;
using System.Collections;
[SelectionBase]
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(CharacterController))]
public class Move : MonoBehaviour {
[SerializeField, DisappearAttachedField]
Animator animator;
[SerializeField, DisappearAttachedField]
CharacterController characterController;
private Vector3 direction;
void Reset()
{
animator = GetComponent<Animator> ();
animator.applyRootMotion = false;
characterController = GetComponent<CharacterController> ();
characterController.center = Vector3.up * 0.75f;
characterController.height = 1.5f;
characterController.radius = 0.3f;
}
void Update ()
{
var cameraForward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized;
direction = cameraForward * Input.GetAxis ("Vertical") +
Camera.main.transform.right * Input.GetAxis ("Horizontal");
animator.SetFloat ("Speed", direction.sqrMagnitude);
animator.SetBool("IsInFall", characterController.isGrounded == false);
}
void OnAnimatorMove()
{
characterController.SimpleMove (direction.normalized * 3);
if (direction != Vector3.zero) {
var velocity = characterController.velocity;
velocity.y = 0;
transform.rotation = Quaternion.LookRotation(velocity);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment