查看: 599|回复: 0
打印 上一主题 下一主题

[其他] Unity3D中CS下蹲脚本

[复制链接]

9903

主题

126

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
53464
精华
316

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2015-8-28 19:26:00 |只看该作者 |倒序浏览

unity3d中CS下蹲脚本:
  1. using UnityEngine;
  2. using System.Collections;
  3. public class Crouch : MonoBehaviour {
  4. private float crouchHeight;
  5. private float standarHeight;
  6. private Vector3 cameraPos;
  7. private GameObject camara;
  8. private Vector3 cameraCpos;
  9. private CharacterController controller;
  10. // Use this for initialization
  11. void Start ()
  12. {
  13. camara = GameObject.FindGameObjectWithTag ("MainCamera");
  14. controller = GetComponent();
  15. standarHeight = controller.height;
  16. crouchHeight = standarHeight/2.5f;
  17. cameraPos = camara.transform.localPosition;
  18. cameraCpos = new Vector3 (cameraPos.x, cameraPos.y/2, cameraPos.z);
  19. }
  20. void Crouching()
  21. {
  22. if (controller.isGrounded)
  23. {
  24. controller.height = crouchHeight;
  25. controller.center = new Vector3 (0f, -0.5f, 0f);
  26. camara.transform.localPosition = cameraCpos;
  27. }
  28. }
  29. void GetUp()
  30. {
  31. transform.position = new Vector3 (transform.position.x, transform.position.y + 0.3f, transform.position.z);
  32. controller.center = new Vector3 (0f, 0f, 0f);
  33. controller.height = standarHeight;
  34. camara.transform.localPosition = cameraPos;
  35. }
  36. // Update is called once per frame
  37. void Update ()
  38. {
  39. if (Input.GetKey (KeyCode.C))
  40. {
  41. Crouching();
  42. }
  43. if (Input.GetKeyUp (KeyCode.C))
  44. {
  45. GetUp();
  46. }
  47. }
  48. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-14 23:18 , Processed in 0.078486 second(s), 30 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部