C# 为什么我的游戏对象不会被拖动和捕捉?
我正在统一制作一个简单的益智游戏。我有两个对象和两个目标。-我写了脚本将我的对象拖到它们的目标位置。我希望我的对象捕捉到不同的位置。我的代码不允许我拖动游戏对象。我只能移动其中一个对象,即使我移动了它。我不能拖。它只是在跳转到其他对象的目标位置一秒钟后出现在目标位置C# 为什么我的游戏对象不会被拖动和捕捉?,c#,unity3d,C#,Unity3d,我正在统一制作一个简单的益智游戏。我有两个对象和两个目标。-我写了脚本将我的对象拖到它们的目标位置。我希望我的对象捕捉到不同的位置。我的代码不允许我拖动游戏对象。我只能移动其中一个对象,即使我移动了它。我不能拖。它只是在跳转到其他对象的目标位置一秒钟后出现在目标位置 using System.Collections; using System.Collections.Generic; using UnityEngine; public class moveObject : MonoBehavi
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class moveObject : MonoBehaviour
{
[SerializeField] private Transform objectPosition;/// target position
private Vector3 initialPosition; ///objects return position if placed wrong
private float deltaX, deltaY;
public static bool correctPosition; /// lock item if it is snapped to true location
public float snapdifficulty; /// snapping range
void Start()
{
initialPosition = transform.position;
}
void Update()
{
if (Input.touchCount > 0 && !correctPosition)
{
Touch touch = Input.GetTouch(0);
Vector2 touchPos = Camera.main.ScreenToWorldPoint(touch.position);
Ray raycast = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit raycastHit;
switch (touch.phase)
{
case TouchPhase.Began:
if (Physics.Raycast(raycast, out raycastHit))
{
if (raycastHit.collider.name == this.name)
{
deltaX = touchPos.x;
deltaY = touchPos.y;
}
}
break;
case TouchPhase.Moved:
if (Physics.Raycast(raycast, out raycastHit))
{
if (raycastHit.collider.name == this.name)
{
Debug.Log("move " + " " + this.name);
transform.position = new Vector3(touchPos.x = deltaX, touchPos.y = deltaY, initialPosition.z);
}
}
break;
case TouchPhase.Ended:
if (Mathf.Abs(transform.position.x - objectPosition.position.x) <= snapdifficulty
&& Mathf.Abs(transform.position.y - objectPosition.position.y) <= snapdifficulty)
{
transform.position = new Vector3(objectPosition.position.x, objectPosition.position.y, initialPosition.z);
correctPosition = true;
}
else
{
transform.position = new Vector3(initialPosition.x, initialPosition.y, initialPosition.z);
}
break;
}
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类moveObject:单行为
{
[SerializeField]私有转换objectPosition;///目标位置
私有向量3 initialPosition;///如果放置错误,对象返回位置
私人浮动deltaX,deltaY;
public static bool correctPosition;///如果项目捕捉到真实位置,则锁定该项目
公共浮点捕捉困难;///捕捉范围
void Start()
{
initialPosition=transform.position;
}
无效更新()
{
如果(Input.touchCount>0&!correctPosition)
{
Touch-Touch=Input.GetTouch(0);
Vector2 touchPos=摄像头.主屏幕到世界点(touch.位置);
光线投射=Camera.main.screenpointoray(Input.GetTouch(0.position));
RaycastHit RaycastHit;
开关(接触相)
{
案例接触阶段。开始:
if(Physics.Raycast(Raycast,out Raycast hit))
{
if(raycastHit.collider.name==this.name)
{
deltaX=触摸位置x;
deltaY=触摸位置y;
}
}
打破
案例接触阶段。移动:
if(Physics.Raycast(Raycast,out Raycast hit))
{
if(raycastHit.collider.name==this.name)
{
Log(“move”+“”+this.name);
transform.position=new Vector3(touchPos.x=deltaX,touchPos.y=deltaY,initialPosition.z);
}
}
打破
案件审理阶段结束:
如果(Mathf.Abs(transform.position.x-objectPosition.position.x)您正在通过
transform.position = new Vector3(touchPos.x = deltaX, touchPos.y = deltaY, initialPosition.z);
在
case TouchPhase.Moved:
if (Physics.Raycast(raycast, out raycastHit))
{
if (raycastHit.collider.name == this.name)
{
Debug.Log("move " + " " + this.name);
transform.position = new Vector3(touchPos.x = deltaX, touchPos.y = deltaY, initialPosition.z);
}
}
break;
这会使您的对象停留在触摸开始的初始位置,直到您到达触摸阶段。结束
案例
我想您更希望存储初始增量:
private Vector2 initialDelta;
把它放进去
case TouchPhase.Began:
if (Physics.Raycast(raycast, out raycastHit))
{
if (raycastHit.collider.name == this.name)
{
initialDelta = transform.position - touchPos;
}
}
break;
然后稍后使用此增量来定位对象
case TouchPhase.Moved:
if (Physics.Raycast(raycast, out raycastHit))
{
if (raycastHit.collider.name == this.name)
{
Debug.Log("move " + " " + this.name);
transform.position = touchPos + initialDelta + Vector3.forward * initialPosition.z;
}
}
break;
一般来说
我不会让每个对象都处理光线投射!这是相当昂贵的
而是将脚本放在场景中的一个控制器对象上,并额外存储所击中的对象
然后,您可以将所有可移动对象放入一个特殊图层,并过滤光线投射,使其仅命中该特定图层
例如
// Set via the Inspector
public LayerMask raycastLayers;
private Transform currentlyDraggedObject;
然后把它放进去
case TouchPhase.Began:
if (Physics.Raycast(raycast, out raycastHit, layermask = (int)raycastLayers))
{
currentlyDraggedObject = raycastHit.transform;
initialDelta = currentlyDraggedObject.position - touchPos;
// Your component would now only store and provide public the target position
objectPosition = currentlyDraggedObject.GetComponent<Moveable>().objectPosition;
}
break;
案例接触阶段。开始:
if(Physics.Raycast(Raycast,out Raycast hit,layermask=(int)Raycast层))
{
currentlyDraggedObject=raycastHit.transform;
initialDelta=当前的DraggedObject.position-touchPos;
//您的组件现在将只存储并提供目标位置的公共信息
objectPosition=CurrentlyDragedObject.GetComponent().objectPosition;
}
打破
然后再对拖动的对象执行操作
case TouchPhase.Moved:
if (currentlyDraggedObject)
{
Debug.Log("move " + " " + currentlyDraggedObject.name);
currentlyDraggedObject.position = touchPos + initialDelta + Vector3.forward * initialPosition.z;
}
break;
case TouchPhase.Ended:
if(currentlyDraggedObject)
{
if (Mathf.Abs(currentlyDraggedObject.position.x - objectPosition.position.x) <= snapdifficulty
&& Mathf.Abs(currentlyDraggedObject.position.y - objectPosition.position.y) <= snapdifficulty)
{
currentlyDraggedObject.position = new Vector3(objectPosition.position.x, objectPosition.position.y, initialPosition.z);
correctPosition = true;
}
else
{
currentlyDraggedObject.position = new Vector3(initialPosition.x, initialPosition.y, initialPosition.z);
}
currentlyDraggedObject = null;
}
break;
案例接触阶段。已移动:
if(当前DraggedObject)
{
Log(“move”+“”+currentlyDraggedObject.name);
currentlyDraggedObject.position=touchPos+initialDelta+Vector3.forward*initialPosition.z;
}
打破
案件审理阶段结束:
if(当前DraggedObject)
{
如果(Mathf.Abs(currentlyDraggedObject.position.x-objectPosition.position.x)您正在通过
transform.position = new Vector3(touchPos.x = deltaX, touchPos.y = deltaY, initialPosition.z);
在
case TouchPhase.Moved:
if (Physics.Raycast(raycast, out raycastHit))
{
if (raycastHit.collider.name == this.name)
{
Debug.Log("move " + " " + this.name);
transform.position = new Vector3(touchPos.x = deltaX, touchPos.y = deltaY, initialPosition.z);
}
}
break;
这会使您的对象停留在触摸开始的初始位置,直到您到达触摸阶段。结束
案例
我想您更希望存储初始增量:
private Vector2 initialDelta;
把它放进去
case TouchPhase.Began:
if (Physics.Raycast(raycast, out raycastHit))
{
if (raycastHit.collider.name == this.name)
{
initialDelta = transform.position - touchPos;
}
}
break;
然后稍后使用此增量来定位对象
case TouchPhase.Moved:
if (Physics.Raycast(raycast, out raycastHit))
{
if (raycastHit.collider.name == this.name)
{
Debug.Log("move " + " " + this.name);
transform.position = touchPos + initialDelta + Vector3.forward * initialPosition.z;
}
}
break;
一般来说
我不会让每个对象都处理光线投射!这是相当昂贵的
而是将脚本放在场景中的一个控制器对象上,并额外存储所击中的对象
然后,您可以将所有可移动对象放入一个特殊图层,并过滤光线投射,使其仅命中该特定图层
例如
// Set via the Inspector
public LayerMask raycastLayers;
private Transform currentlyDraggedObject;
然后把它放进去
case TouchPhase.Began:
if (Physics.Raycast(raycast, out raycastHit, layermask = (int)raycastLayers))
{
currentlyDraggedObject = raycastHit.transform;
initialDelta = currentlyDraggedObject.position - touchPos;
// Your component would now only store and provide public the target position
objectPosition = currentlyDraggedObject.GetComponent<Moveable>().objectPosition;
}
break;
案例接触阶段。开始:
if(Physics.Raycast(Raycast,out Raycast hit,layermask=(int)Raycast层))
{
currentlyDraggedObject=raycastHit.transform;
initialDelta=当前的DraggedObject.position-touchPos;
//您的组件现在将只存储并提供目标位置的公共信息
objectPosition=CurrentlyDragedObject.GetComponent().objectPosition;
}
打破
然后再对拖动的对象执行操作
case TouchPhase.Moved:
if (currentlyDraggedObject)
{
Debug.Log("move " + " " + currentlyDraggedObject.name);
currentlyDraggedObject.position = touchPos + initialDelta + Vector3.forward * initialPosition.z;
}
break;
case TouchPhase.Ended:
if(currentlyDraggedObject)
{
if (Mathf.Abs(currentlyDraggedObject.position.x - objectPosition.position.x) <= snapdifficulty
&& Mathf.Abs(currentlyDraggedObject.position.y - objectPosition.position.y) <= snapdifficulty)
{
currentlyDraggedObject.position = new Vector3(objectPosition.position.x, objectPosition.position.y, initialPosition.z);
correctPosition = true;
}
else
{
currentlyDraggedObject.position = new Vector3(initialPosition.x, initialPosition.y, initialPosition.z);
}
currentlyDraggedObject = null;
}
break;
案例接触阶段。已移动:
if(当前DraggedObject)
{
Log(“move”+“”+currentlyDraggedObject.name);
currentlyDraggedObject.position=touchPos+initialDelta+Vector3.forward*initialPosition.z;
}
打破
案件审理阶段结束:
if(当前DraggedObject)
{
if(Mathf.Abs(currentlyDraggedObject.position.x-objectPosition.position.x)我不知道我应该把它们分层谢谢!也就是说。当我触摸我的物体并移动时,物体的位置只更新了一次,并且根据delta移动它似乎现在就把它发送到了一个不相关的位置。我不知道我应该把它们分层谢谢!也就是说。Wh