我试图在我的游戏中允许多点触控。我有两个脚本,一个处理相机旋转,另一个处理 GUITexture 点击。问题是,当有人移动相机时,他们无法同时推动 GUITexture(发射子弹)。
我尝试了多种合并两个脚本的方法,但我没有让它工作。我找到了一些方法来做实际的多点触控,我只是不确定如何将它合并到我的游戏中(因为我使用了多个脚本).. 这是多点触控信息的链接:http: //answers.unity3d.com/questions/167750/how-does-multi-touch-work.html
谢谢!!
编辑(这里是脚本):
消防按钮脚本:
#pragma strict
//Fire Button Vars
var myImg : GUITexture;
var touches = Input.touches;
var newBullet : Rigidbody;
var throwSpeed : float = 30.0;
function Start () {
}
function Update () {
var tapCount = Input.touchCount;
if(tapCount > 1) {
var touch1 = Input.GetTouch(0);
var touch2 = Input.GetTouch(1);
//Fire Button
if (myImg.HitTest(Input.GetTouch(1).position))
{
if(Input.GetTouch(1).phase==TouchPhase.Began)
{
print("Touch has began on image");
var newBullet : Rigidbody = Instantiate(newBullet, transform.position, transform.rotation);
newBullet.velocity = transform.forward * throwSpeed;
}
if(Input.GetTouch(1).phase==TouchPhase.Stationary)
{
print("Touch is on image");
}
if(Input.GetTouch(1).phase==TouchPhase.Moved)
{
print("Touch is moving on image");
}
if(Input.GetTouch(1).phase==TouchPhase.Ended)
{
print("Touch has been ended on image");
}else{
// if (myImg.HitTest(Input.GetTouch(1).position))
// {
// if(Input.GetTouch(1).phase==TouchPhase.Began)
/* {
print("Touch has began on image");
var newBullet2 : Rigidbody = Instantiate(newBullet, transform.position, transform.rotation);
newBullet.velocity = transform.forward * throwSpeed;
}
if(Input.GetTouch(0).phase==TouchPhase.Stationary)
{
print("Touch is on image");
}
if(Input.GetTouch(0).phase==TouchPhase.Moved)
{
print("Touch is moving on image");
}
if(Input.GetTouch(0).phase==TouchPhase.Ended)
{
print("Touch has been ended on image");
}
*/ }
}
}
}
相机旋转脚本(此外,下面的脚本仅在您将手指拖到地形或“实体”上时才有效。如果您只是清空空间(例如天空),它就不起作用......任何让它可以在任何地方工作的方法当有人触摸屏幕时?):
#pragma strict
var targetItem : GameObject;
var GUICamera : Camera;
var ambient : GameObject;
var max = 90;
var min = 270;
/********Rotation Variables*********/
var rotationRate : float = 1.0;
private var wasRotating;
/************Scrolling inertia variables************/
private var scrollPosition : Vector2 = Vector2.zero;
private var scrollVelocity : float = 0;
private var timeTouchPhaseEnded: float;
private var inertiaDuration : float = 0.5f;
private var itemInertiaDuration : float = 1.0f;
private var itemTimeTouchPhaseEnded: float;
private var rotateVelocityX : float = 0;
private var rotateVelocityY : float = 0;
var hit: RaycastHit;
private var layerMask = (1 << 8) | (1 << 2);
//private var layerMask = (1 << 0);
function Start()
{
layerMask =~ layerMask;
}
function FixedUpdate()
{
if (Input.touchCount > 0)
{ // If there are touches...
var theTouch : Touch = Input.GetTouch(0); // Cache Touch (0)
var ray = Camera.main.ScreenPointToRay(theTouch.position);
var GUIRayq = GUICamera.ScreenPointToRay(theTouch.position);
if(Physics.Raycast(ray,hit,50,layerMask))
{
if(Input.touchCount == 1)
{
if (theTouch.phase == TouchPhase.Began)
{
wasRotating = false;
}
if (theTouch.phase == TouchPhase.Moved)
{
targetItem.transform.Rotate(0, theTouch.deltaPosition.x * rotationRate,0,Space.World);
wasRotating = true;
var angle = theTouch.deltaPosition.x * rotationRate;
if (angle > max){
angle = max;
}
else if (angle < min){
angle = min;
}
}
}
}
}
}