LoginSignup
19
18

More than 5 years have passed since last update.

【Unity】バイオハザード風の動きをするscriptとカメラについて

Posted at

はじめに

ネタが無い!っていうか思いつかない!
でも何か書きたい!ので書きます。
使ってる前提で書いちゃいます。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ちゃんがバイオハザードごっこをしている様子です。
スクリーンショット 2014-04-17 23.02.34.png

こんなんでいいのかよ!もっとこうあるだろ…!複数のライティングはコスト高いからモバイルでは気をつけろ!とか…。上手いこと説明出来るようにまとめてからまた書きます。

以上

19
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
18