纳金网
标题:
Unity3D中CS下蹲脚本
[打印本页]
作者:
烟雨
时间:
2015-8-28 19:26
标题:
Unity3D中CS下蹲脚本
Unity3D中CS下蹲脚本:
using UnityEngine;
using System.Collections;
public class Crouch : MonoBehaviour {
private float crouchHeight;
private float standarHeight;
private Vector3 cameraPos;
private GameObject camara;
private Vector3 cameraCpos;
private CharacterController controller;
// Use this for initialization
void Start ()
{
camara = GameObject.FindGameObjectWithTag ("MainCamera");
controller = GetComponent();
standarHeight = controller.height;
crouchHeight = standarHeight/2.5f;
cameraPos = camara.transform.localPosition;
cameraCpos = new Vector3 (cameraPos.x, cameraPos.y/2, cameraPos.z);
}
void Crouching()
{
if (controller.isGrounded)
{
controller.height = crouchHeight;
controller.center = new Vector3 (0f, -0.5f, 0f);
camara.transform.localPosition = cameraCpos;
}
}
void GetUp()
{
transform.position = new Vector3 (transform.position.x, transform.position.y + 0.3f, transform.position.z);
controller.center = new Vector3 (0f, 0f, 0f);
controller.height = standarHeight;
camara.transform.localPosition = cameraPos;
}
// Update is called once per frame
void Update ()
{
if (Input.GetKey (KeyCode.C))
{
Crouching();
}
if (Input.GetKeyUp (KeyCode.C))
{
GetUp();
}
}
}
复制代码
欢迎光临 纳金网 (http://wwww.narkii.com/club/)
Powered by Discuz! X2.5