Skip to content

Instantly share code, notes, and snippets.

@jake1256
Created April 17, 2014 14:07
Show Gist options
  • Save jake1256/10986176 to your computer and use it in GitHub Desktop.
Save jake1256/10986176 to your computer and use it in GitHub Desktop.
【Unity】バイオハザード風の動きをするscriptとカメラについて ref: http://qiita.com/kuuki_yomenaio/items/1eac012d5e40ce47b4e2
#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 );
}
#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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment