找回密码
 立即注册
查看: 5867|回复: 92

[脚本] CS下蹲脚本

[复制链接]
发表于 2013-2-2 21:28 | 显示全部楼层 |阅读模式
[code=csharp]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();
    }
  }
}
[/code]

评分

参与人数 1鲜花 +1 收起 理由
失却 + 1

查看全部评分

发表于 2013-2-25 14:43 | 显示全部楼层
不错...思路清晰.
发表于 2013-3-4 10:41 | 显示全部楼层

膜拜中。。。。{:soso__7524161091986203637_5:}
发表于 2017-3-17 13:41 | 显示全部楼层
好帖就是要顶
发表于 2017-3-17 13:38 | 显示全部楼层
难得一见的好帖
发表于 2017-3-17 12:52 | 显示全部楼层
说的非常好
发表于 2017-3-17 12:56 | 显示全部楼层
很好哦
发表于 2017-3-17 13:16 | 显示全部楼层
LZ真是人才
发表于 2017-4-23 12:35 | 显示全部楼层
很不错
发表于 2017-4-23 12:47 | 显示全部楼层
顶顶多好
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-24 19:24 , Processed in 0.162854 second(s), 29 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表