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<CharacterController>();
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();
}
}
}
不错...思路清晰.
膜拜中。。。。{:soso__7524161091986203637_5:} 好帖就是要顶 难得一见的好帖 说的非常好 很好哦 LZ真是人才 很不错 顶顶多好
页:
[1]