Unity3D 之连按移动加速
2016-08-11 20:38
155 查看
上代码:
效果是连续按W后,加速移动
为物体添加个拖拽效果,方便看运动轨迹。
将下面的脚本绑定到移动的物体上。
不过这里有一点很重要的需要去注意就是该方法不能放在 void FixedUpdate() 中,否则无效
效果是连续按W后,加速移动
为物体添加个拖拽效果,方便看运动轨迹。
将下面的脚本绑定到移动的物体上。
不过这里有一点很重要的需要去注意就是该方法不能放在 void FixedUpdate() 中,否则无效
using UnityEngine; using System.Collections; public class Move : MonoBehaviour { public float timelost = 0; // Use this for initialization public float speed = 1f; void Awake() { //animator = this.GetComponent<Animator>(); } // Update is called once per frame void Update() { float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); if (Input.GetKeyDown(KeyCode.W)) // if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.S)) { if ((Time.time - timelost) < 0.5f)///0.5秒之内按下有效 { Debug.LogError("加速"); speed = 5.0f; } else { Debug.LogError("--------"); speed = 1f; } timelost = Time.time; } Debug.LogError("》》》》》》》》》》》》》》》"); if (Mathf.Abs(h) > 0.1f || Mathf.Abs(v) > 0.1) { Vector3 targetDir = new Vector3(-h, 0, -v); //transform.LookAt(targetDir + transform.position); transform.Translate(targetDir * speed * Time.deltaTime); } //按键的取值,以虚拟杆中的值为优先 //if (Mathf.Abs(h) > 0.1f || Mathf.Abs(v) > 0.1) //{ // // Debug.LogError("h" + h); // //Debug.LogError("v" + v); // //animator.SetBool("Walk", true); // Vector3 targetDir = new Vector3(-h, 0,-v); // //transform.LookAt(targetDir + transform.position); // transform.Translate(targetDir * speed *Time.deltaTime); //} } }
相关文章推荐
- Unity3D控制人物移动跳跃,处理CharacterController的isGrounded总是返回false问题
- 细说Unity3D——移动平台动态读取外部文件全解析(转)
- Unity3D控制物体移动
- 细说Unity3D——移动平台动态读取外部文件全解析
- Unity3D 几个基本动画(控制物体移动、旋转、缩放)
- Unity3D游戏场景之鼠标点击地面控制人物移动
- Unity3D让物体从规定时间内移动到某一位置
- Unity3D游戏制作 移动平台上的角色阴影制作
- 【OSTC讲师专访】腾讯追风(QcloudMna):开源移动加速SDK
- unity3d射线控制移动
- unity3d和下OrtHello的使用教程 (二) 移动物体,碰撞等
- unity3d中使用DoTween来控制2D摄像机视口的移动
- unity3d移动及键鼠状态
- 汲取新知·加速未来 2006微软移动与嵌入式开发者大会即将召开
- 移动平台Unity3D 应用性能优化
- unity3d 让物体移动到点击位置
- Unity3D移动平台动态读取外部文件全解析
- unity3d,制作自己的第一个射击游戏demo,实现镜头跟随和键盘控制移动
- unity3d移动平台优化专题(2):不要使用实时光照
- Unity3D 利用character controller控制人物在场景中移动和利用Navmesh Agent设置人物自动寻路