Unity3d 统一:触摸延迟太高
我想做一个游戏,在那里你可以通过拖放在2D中投掷物品。这很好,但是抓取它们的跟踪非常糟糕。如果我在上半部分触碰它们,跟踪将无法或几乎无法工作 有没有其他方法来防止这种情况Unity3d 统一:触摸延迟太高,unity3d,Unity3d,我想做一个游戏,在那里你可以通过拖放在2D中投掷物品。这很好,但是抓取它们的跟踪非常糟糕。如果我在上半部分触碰它们,跟踪将无法或几乎无法工作 有没有其他方法来防止这种情况 public class DropsBehaviour : MonoBehaviour { //Declaring Variables public int force = 10; float maxSpeedPreset = 5; float maxSpeed = 5; float M
public class DropsBehaviour : MonoBehaviour {
//Declaring Variables
public int force = 10;
float maxSpeedPreset = 5;
float maxSpeed = 5;
float MaxThrowSpeed = 8;
GameManager gm;
Rigidbody rig;
Camera cam;
bool selected = false;
bool once = false;
void Start() {
gm = GameManager.FindMe();
rig = GetComponent<Rigidbody>();
cam = Camera.main;
}
void Update() {
if (selected) {
rig.AddForce((cam.ScreenToWorldPoint(Input.mousePosition) - transform.position) * force);
rig.velocity = Vector3.zero;
} else {
if (!once) {
maxSpeed = maxSpeedPreset * gm.gameSpeed;
rig.velocity = new Vector3(Mathf.Clamp(rig.velocity.x, -maxSpeed, maxSpeed), Mathf.Clamp(rig.velocity.y, -maxSpeed, maxSpeed), Mathf.Clamp(rig.velocity.z, -maxSpeed, maxSpeed));
} else {
maxSpeed = maxSpeedPreset;
rig.velocity = new Vector3(Mathf.Clamp(rig.velocity.x, -MaxThrowSpeed, MaxThrowSpeed), Mathf.Clamp(rig.velocity.y, -MaxThrowSpeed, MaxThrowSpeed), Mathf.Clamp(rig.velocity.z, -MaxThrowSpeed, MaxThrowSpeed));
}
}
}
void OnMouseDown() {
if (tag != "NoInteract") {
selected = true;
once = true;
}
}
void OnMouseUp() {
selected = false;
}
}
公共类DropsBehaviour:单一行为{
//声明变量
公共警力=10;
浮点最大速度预设=5;
浮点最大速度=5;
浮点最大throwspeed=8;
游戏经理总经理;
刚体钻机;
摄像机;
bool selected=false;
bool once=假;
void Start(){
gm=GameManager.FindMe();
rig=GetComponent();
cam=Camera.main;
}
无效更新(){
如果(选定){
rig.AddForce((凸轮屏幕到世界点(输入鼠标位置)-变换位置)*force);
rig.velocity=矢量3.0;
}否则{
如果(!一次){
maxSpeed=maxSpeedPreset*gm.gameSpeed;
rig.velocity=新矢量3(Mathf.Clamp(rig.velocity.x,-maxSpeed,maxSpeed),Mathf.Clamp(rig.velocity.y,-maxSpeed,maxSpeed),Mathf.Clamp(rig.velocity.z,-maxSpeed,maxSpeed));
}否则{
maxSpeed=maxSpeedPreset;
rig.velocity=新矢量3(Mathf.Clamp(rig.velocity.x,-MaxThrowSpeed,MaxThrowSpeed),Mathf.Clamp(rig.velocity.y,-MaxThrowSpeed,MaxThrowSpeed),Mathf.Clamp(rig.velocity.z,-MaxThrowSpeed,MaxThrowSpeed));
}
}
}
void OnMouseDown(){
如果(标记!=“无交互”){
所选=真;
一次=真;
}
}
void OnMouseUp(){
所选=假;
}
}
我在您发布的代码片段中看到了很多可能导致口吃的问题
首先,如果要在2D中制作某些东西,建议使用Rigidbody2D和Collider2D
然后,不应该手动编辑刚体的速度。这就是加力的作用。
当更新任何与物理相关的内容时,应该在FixedUpdate中执行,而不是更新
我猜你也忘了在第一关上写“once=false”?我真的不确定这个
我按照前面提到的要点编辑了您的脚本,同时保持刚体为3D。我没有测试过它,但希望它能纠正你正在经历的奇怪的拖动行为!(请注意,我删除了GameManager部分,因为我不知道它的功能)
公共类DropsBehaviour:单一行为
{
公共警力=10;
private const float MaxSpeedPreset=5;
专用浮点数_maxSpeed=5;
private const float MaxThrowSpeed=8;
私人刚体钻机;
私人摄像机;
选择私人布尔;
一次私人聚会;
专用矢量3_targetVelocity;
私有void Start()
{
_rig=GetComponent();
_cam=Camera.main;
}
私有void更新()
{
如果选择(_)返回;
如果(!\u一次)
{
_maxSpeed=MaxSpeedPreset*10;
_targetVelocity=新矢量3(数学钳位(_rig.velocity.x,--u maxSpeed,_maxSpeed),
数学夹具(_装备速度.y,-_最大速度,_最大速度),
数学夹具(_rig.velocity.z,_maxSpeed,_maxSpeed));
}
其他的
{
_maxSpeed=MaxSpeedPreset;
_targetVelocity=新矢量3(数学钳位(_rig.velocity.x,-MaxThrowSpeed,MaxThrowSpeed),
数学夹具(_rig.velocity.y,-MaxThrowSpeed,MaxThrowSpeed),
数学夹具(_rig.velocity.z,-MaxThrowSpeed,MaxThrowSpeed));
_once=false;//不知道这是不是有意的?
}
}
私有void FixedUpdate()
{
如果(_选中)
{
_rig.AddForce((_cam.ScreenToWorldPoint(Input.mousePosition)-变换位置)*force);
_rig.velocity=矢量3.0;
返回;
}
var velocity=_rig.velocity;
var velocityChange=_targetVelocity-速度;
_rig.AddForce(速度变化、力模式、速度变化);
}
私有void OnMouseDown()
{
if(CompareTag(“NoInteract”))返回;
_所选=真;
_一次=真;
}
私有void OnMouseUp()
{
_所选=假;
}
}
顺便说一句,我个人不喜欢标签,我会用a来代替,因为它有点和物理有关。我可能也会用一个光线投射来代替OnMouse事件,这样你就可以更好地控制物理方面发生的事情。你好,我试过了,但没有任何效果。我按下了视频中的每个立方体,这里是一个视频
public class DropsBehaviour : MonoBehaviour
{
public int force = 10;
private const float MaxSpeedPreset = 5;
private float _maxSpeed = 5;
private const float MaxThrowSpeed = 8;
private Rigidbody _rig;
private Camera _cam;
private bool _selected;
private bool _once;
private Vector3 _targetVelocity;
private void Start()
{
_rig = GetComponent<Rigidbody>();
_cam = Camera.main;
}
private void Update()
{
if (_selected) return;
if (!_once)
{
_maxSpeed = MaxSpeedPreset * 10;
_targetVelocity = new Vector3(Mathf.Clamp(_rig.velocity.x, -_maxSpeed, _maxSpeed),
Mathf.Clamp(_rig.velocity.y, -_maxSpeed, _maxSpeed),
Mathf.Clamp(_rig.velocity.z, -_maxSpeed, _maxSpeed));
}
else
{
_maxSpeed = MaxSpeedPreset;
_targetVelocity = new Vector3(Mathf.Clamp(_rig.velocity.x, -MaxThrowSpeed, MaxThrowSpeed),
Mathf.Clamp(_rig.velocity.y, -MaxThrowSpeed, MaxThrowSpeed),
Mathf.Clamp(_rig.velocity.z, -MaxThrowSpeed, MaxThrowSpeed));
_once = false; //Dunno if it's intended or not ?
}
}
private void FixedUpdate()
{
if (_selected)
{
_rig.AddForce((_cam.ScreenToWorldPoint(Input.mousePosition) - transform.position) * force);
_rig.velocity = Vector3.zero;
return;
}
var velocity = _rig.velocity;
var velocityChange = _targetVelocity - velocity;
_rig.AddForce(velocityChange, ForceMode.VelocityChange);
}
private void OnMouseDown()
{
if (CompareTag("NoInteract")) return;
_selected = true;
_once = true;
}
private void OnMouseUp()
{
_selected = false;
}
}