首页 技术 正文
技术 2022年11月15日
0 收藏 882 点赞 2,400 浏览 1783 个字
using UnityEngine;using System.Collections;public class MyController : MonoBehaviour {    ;    ;    ;    private CharacterMotor motor;    private float dist;//distance to ground    // Use this for initialization    void Start () {        motor = GetComponent<CharacterMotor>();        CharacterController ch = GetComponent<CharacterController>();        dist = ch.height / ;    }    void FixedUpdate()    {        float vScale = 1.0f;        float speed = walkSpeed;        if ((Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift)) &&        Input.GetKey(KeyCode.W))        {            speed = runSpeed;        }        if (Input.GetKey(KeyCode.C))        {            vScale = 0.5f;            speed = crouchSpeed;        }        motor.movement.maxForwardSpeed = speed;//set max speed        float ultScale = transform.localScale.y;//crouch stand up smoothly        Vector3 tmpScale = transform.localScale;        Vector3 tmpPosition = transform.position;        tmpScale.y = Mathf.Lerp(transform.localScale.y, vScale,  * Time.deltaTime);        transform.localScale = tmpScale;        tmpPosition.y += dist * (transform.localScale.y - ultScale);//fix vertical position        transform.position = tmpPosition;    }}

本文转自其它网站。添加了First Person Controller后,直接挂上代码即可实现跑步与下蹲。下面是相应的JavaScript代码:

var walkSpeed: float = 7; // regular speedvar crchSpeed: float = 3; // crouching speedvar runSpeed: float = 20; // run speedprivate var chMotor: CharacterMotor;private var ch: CharacterController;private var tr: Transform;private var height: float; // initial heightfunction Start(){    chMotor = GetComponent(CharacterMotor);    tr = transform;    ch = GetComponent(CharacterController);    height = ch.height;}function Update(){    var h = height;    var speed = walkSpeed;    if (ch.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift")){        speed = runSpeed;    }    if (Input.GetKey("c")){ // press C to crouch        h = 0.5 * height;        speed = crchSpeed; // slow down when crouching    }    chMotor.movement.maxForwardSpeed = speed; // set max speed    var lastHeight = ch.height; // crouch/stand up smoothly    ch.height = Mathf.Lerp(ch.height, h, 5*Time.deltaTime);    tr.position.y += (ch.height-lastHeight)/2; // fix vertical position}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,961
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,485
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,330
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,113
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,746
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,780