はじめに
ネタが無い!っていうか思いつかない!
でも何か書きたい!ので書きます。
使ってる前提で書いちゃいます。tips系ですね。
バイオハザード風の動きをするコントローラー
バイオハザートと言っても4〜のド派手なアクションではなく、
1〜3のあの動きになります。
コントローラーの上下で前後、左右で向きを変える、という挙動です。
UnityScriptで書いちゃってますが、C# , Booでも基本同じです。
CharacterController_Bio.js
#pragma strict
public var speed : float = 3;
private var direction : Vector3 = Vector3.zero;
private var playerController : CharacterController;
private var animator : Animator;
function Start() {
playerController = GetComponent( CharacterController );
animator = GetComponentInChildren( Animator );
}
function Update() {
if( playerController.isGrounded ) {
// 上下キーが押されたら
if(Input.GetAxis("Vertical") != 0){
var varticalMove = Input.GetAxis("Vertical");
animator.SetFloat("Speed" , varticalMove);
direction = new Vector3(0 , 0 , varticalMove);
direction = transform.TransformDirection(direction);
direction *= speed;
}
// 左右キーが押されたら
if(Input.GetAxis("Horizontal") != 0){
transform.Rotate(0 , 100 * Time.deltaTime * Input.GetAxis("Horizontal") , 0);
}
}
direction.y += Physics.gravity.y * Time.deltaTime;
playerController.Move( direction * Time.deltaTime );
}
カメラはどうする?
カメラは標準のSmoothFollow.jsでもいいでしょう。左右の回転をカメラがちゃんと追ってくれます。
もし、自分でカスタムしたい場合のためにソース置いときます。自分はコッチ使ってます。
(毎回パラメータいじるのめんどいので)
SmoothFollow_Bio.js
#pragma strict
var target : Transform;
var distance = 3.0;
var height = 2.0;
var heightDamping = 0;
var rotationDamping = 1.0;
function LateUpdate () {
if (!target) return;
// Calculate the current rotation angles
var wantedRotationAngle = target.eulerAngles.y;
var wantedHeight = target.position.y + height;
var currentRotationAngle = transform.eulerAngles.y;
var currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position.y = currentHeight;
// Always look at the target
transform.LookAt (target);
}
暗い洋館を探索するUnityちゃんがバイオハザードごっこをしている様子です。
こんなんでいいのかよ!もっとこうあるだろ…!複数のライティングはコスト高いからモバイルでは気をつけろ!とか…。上手いこと説明出来るようにまとめてからまた書きます。
以上