限制物体在一定区域内移动(修改BUG版)
2018-02-05 17:31
375 查看
前两天写过一篇这样的文章,最后发现有一点小小的BUG(这个BUG很隐蔽的,特殊操作下,会穿过限制点),这里从新修改一下,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarTrigger : MonoBehaviour {
public GameObject Point1;//限制点1
public GameObject Point2;//限制点2
public float carSpeed = 2f;//移动速度
public bool WTrigger = false;//是否是触发状态
public bool STrigger = false;
void Update () {
float v = Input.GetAxis("Vertical");//垂直向量的输入
if (WTrigger == false&&Input.GetKey(KeyCode.W))//如果不是触发状态
{
//模型移动
float smooth = v * carSpeed * Time.deltaTime;
transform.Translate(0, 0, smooth);//往哪个轴的,我这里是z轴的方向
}
if (STrigger == false && Input.GetKey(KeyCode.S))//如果不是触发状态
{
//模型移动
float smooth = v * carSpeed * Time.deltaTime;
transform.Translate(0, 0, smooth);//往哪个轴的,我这里是z轴的方向
}
}
//触发器离开的事件
public void OnTriggerExit(Collider other)
{
if (Input.GetKey(KeyCode.W)) { STrigger = false; }
if (Input.GetKey(KeyCode.S)) { WTrigger = false; }
}
//触发器刚触发产生的事件
public void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "Point1") { STrigger = true; }
if (other.gameObject.name == "Point2") { WTrigger = true; }
}
}
这里就解决了特殊操作下,穿过限制点的BUG了。希望对有需要的人有帮助!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarTrigger : MonoBehaviour {
public GameObject Point1;//限制点1
public GameObject Point2;//限制点2
public float carSpeed = 2f;//移动速度
public bool WTrigger = false;//是否是触发状态
public bool STrigger = false;
void Update () {
float v = Input.GetAxis("Vertical");//垂直向量的输入
if (WTrigger == false&&Input.GetKey(KeyCode.W))//如果不是触发状态
{
//模型移动
float smooth = v * carSpeed * Time.deltaTime;
transform.Translate(0, 0, smooth);//往哪个轴的,我这里是z轴的方向
}
if (STrigger == false && Input.GetKey(KeyCode.S))//如果不是触发状态
{
//模型移动
float smooth = v * carSpeed * Time.deltaTime;
transform.Translate(0, 0, smooth);//往哪个轴的,我这里是z轴的方向
}
}
//触发器离开的事件
public void OnTriggerExit(Collider other)
{
if (Input.GetKey(KeyCode.W)) { STrigger = false; }
if (Input.GetKey(KeyCode.S)) { WTrigger = false; }
}
//触发器刚触发产生的事件
public void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "Point1") { STrigger = true; }
if (other.gameObject.name == "Point2") { WTrigger = true; }
}
}
这里就解决了特殊操作下,穿过限制点的BUG了。希望对有需要的人有帮助!
相关文章推荐
- 在delphi中如何限制鼠标的移动区域。
- 修改mjextension使用一键归档在iOS9下有一定几率会闪退的bug
- draggable()拖拽时限制移动区域
- draggable()拖拽时限制移动区域
- extension是iOS8新开放的一种对几个固定系统区域的扩展机制,它可以在一定程度上弥补iOS的沙盒机制对应用间通信的限制。
- 鼠标/指头移动物体,并限制移动范围笔记实例
- [Unity实战]判断角色面朝一定区域是否存在物体
- 基于python使用opencv监测视频指定区域是否有物体移动
- VRTK学习记录1——使用NavMesh限制移动区域
- 使用JS制作一个鼠标可拖的DIV(二)——限制区域移动
- phpcms后台批量移动用户会员组积分bug修改
- Winform001 Winform中在一定区域内移动控件
- ios UIImage随手势一起移动并限制移动区域
- 限制鼠标移动范围在圆形区域
- gitlab修改最大项目限制
- 在MySQL中修改密码及访问限制的设置方法详解
- Unity3D研究院之主角面朝方向一定区域内对象角度计算
- 修改zxing扫描二维码区域大小
- WP8 POST 方法(转载经过一定的修改)
- 背景图片的适屏(背景图片可以移动但是大小和屏幕一定一样)