- 最后登录
- 2021-7-6
- 注册时间
- 2012-12-27
- 阅读权限
- 90
- 积分
- 76145
- 纳金币
- 53488
- 精华
- 316
|
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();
- }
- }
- }
复制代码 |
|