Skip to content

Instantly share code, notes, and snippets.

@IshidaGames
Created November 12, 2019 12:28
Show Gist options
  • Save IshidaGames/665110ddb43b4ab18778ff1ff7601223 to your computer and use it in GitHub Desktop.
Save IshidaGames/665110ddb43b4ab18778ff1ff7601223 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//CapsuleColliderとRigidbodyを追加
[RequireComponent(typeof(CapsuleCollider))]
[RequireComponent(typeof(Rigidbody))]
public class Old : MonoBehaviour
{
//移動スピード
[SerializeField] float speed = 4f;
//Animatorを入れる
private Animator animator;
//メインカメラのTransformを入れる
Transform cam;
//Rigidbodyを入れる
Rigidbody rb;
//Capsule Colliderを入れる
CapsuleCollider caps;
void Start()
{
//Animatorコンポーネントを取得
animator = GetComponent<Animator>();
//Rigidbodyコンポーネントを取得
rb = GetComponent<Rigidbody>();
//RigidbodyのConstraintsを3つともチェック入れて
//勝手に回転しないようにする
rb.constraints = RigidbodyConstraints.FreezeRotation;
//メインカメラのTransformを取得
cam = Camera.main.transform;
//CapsuleColliderコンポーネントを取得
caps = GetComponent<CapsuleCollider>();
//CapsuleColliderの中心の位置を決める
caps.center = new Vector3(0, 0.76f, 0);
//CapsuleColliderの半径を決める
caps.radius = 0.23f;
//CapsuleColliderの高さを決める
caps.height = 1.6f;
}
void Update()
{
//A・Dキー、←→キーで横移動
float x = Input.GetAxisRaw("Horizontal") * Time.deltaTime * speed;
//W・Sキー、↑↓キーで前後移動
float z = Input.GetAxisRaw("Vertical") * Time.deltaTime * speed;
//AnimatorControllerのParametersに数値を送って
//アニメーションを出す
animator.SetFloat("X", x * 50);
animator.SetFloat("Y", z * 50);
//前移動の時だけ方向転換をさせる
if (z > 0)
{
transform.rotation = Quaternion.Euler(new Vector3(transform.rotation.x,
cam.eulerAngles.y, transform.rotation.z));
}else
//zが0より小さい(後退する)とスピードが落ちる
if(z < 0)
{
z = z / 1.5f;
}
//xとzの数値に基づいて移動
transform.position += transform.forward * z + transform.right * x;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment