神秘入侵者 发表于 2013-2-2 21:28

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();
    }
}
}

Steven 发表于 2013-2-25 14:43

不错...思路清晰.

jimsato 发表于 2013-3-4 10:41


膜拜中。。。。{:soso__7524161091986203637_5:}

LeDows 发表于 2017-3-17 13:41

好帖就是要顶

LeDows 发表于 2017-3-17 13:38

难得一见的好帖

luckzzs 发表于 2017-3-17 12:52

说的非常好

luckzzs 发表于 2017-3-17 12:56

很好哦

下载M~回家 发表于 2017-3-17 13:16

LZ真是人才

╰_魔纞ヽ 发表于 2017-4-23 12:35

很不错

╰_魔纞ヽ 发表于 2017-4-23 12:47

顶顶多好
页: [1]
查看完整版本: CS下蹲脚本