C# UnityEngine.Input.GetTouch(System.Int32索引)
这是一个像弹弓一样的愤怒的小鸟,我需要检测触摸的开始和结束,脚本可以工作,但当我把手指从屏幕上拿开时,错误显示在第24行(我在它旁边放了点)。只有当我不触摸屏幕时,错误才会出现。如果我触摸屏幕,错误就会消失,当我抬起手指时,错误就会回来C# UnityEngine.Input.GetTouch(System.Int32索引),c#,unity3d,unity-remote,C#,Unity3d,Unity Remote,这是一个像弹弓一样的愤怒的小鸟,我需要检测触摸的开始和结束,脚本可以工作,但当我把手指从屏幕上拿开时,错误显示在第24行(我在它旁边放了点)。只有当我不触摸屏幕时,错误才会出现。如果我触摸屏幕,错误就会消失,当我抬起手指时,错误就会回来 public class BallShooting : MonoBehaviour { public Rigidbody2D rb; private bool isPressed = false; public bool isDead =
public class BallShooting : MonoBehaviour
{
public Rigidbody2D rb;
private bool isPressed = false;
public bool isDead = false;
public bool hasShoot = false;
public int Damage;
public int BaseDamage = 1;
public BoxCollider2D DeathZone;
public Transform startingSpot;
public GameObject platform;
public float releaseTime = .15f;
void FixedUpdate()
{
if (Input.touchCount >= 0 && isDead == false)
{
... if (Input.GetTouch(0).phase == TouchPhase.Began)
{
// Debug.Log("TouchBegan");
isPressed = true;
rb.isKinematic = true;
}
if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
// Debug.Log("TouchEnded");
isPressed = false;
rb.isKinematic = false;
StartCoroutine(Release());
}
}
if(isPressed && !hasShoot)
{
Touch touch = Input.GetTouch(0);
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
rb.position = touchPosition;
}
if (isDead)
{
this.transform.position = new Vector3(startingSpot.position.x, startingSpot.position.y, startingSpot.position.z);
isDead = false;
DeathZone.enabled = true;
}
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Ground" && hasShoot == true)
{
isDead = true;
Debug.Log("Dead");
DeathZone.enabled = false;
}
}
IEnumerator Release()
{
yield return new WaitForSeconds(releaseTime);
GetComponent<SpringJoint2D>().enabled = false;
hasShoot = true;
}
}
public class BallShooting:monobhavior
{
公共刚体2d rb;
private bool isPressed=false;
公共bool isDead=false;
公共bool hasShoot=false;
公共财产损害;
公共基础损害=1;
公共区域:R2D死亡区;
公共改造启动点;
公共游戏对象平台;
公共浮动释放时间=0.15f;
void FixedUpdate()
{
如果(Input.touchCount>=0&&isDead==false)
{
…如果(Input.GetTouch(0.phase==TouchPhase.Start)
{
//Debug.Log(“touchStart”);
isPressed=true;
rb.iskinetic=true;
}
if(Input.GetTouch(0.phase==TouchPhase.Ended)
{
//Debug.Log(“TouchEnded”);
isPressed=false;
rb.iskinetic=false;
start例程(Release());
}
}
如果(显示和显示)
{
Touch-Touch=Input.GetTouch(0);
Vector3 touchPosition=Camera.main.Screen到世界点(touch.position);
rb.position=触摸位置;
}
如果(isDead)
{
this.transform.position=新矢量3(startingSpot.position.x,startingSpot.position.y,startingSpot.position.z);
isDead=false;
DeathZone.enabled=true;
}
}
无效OnTiggerEnter2D(碰撞的R2D其他)
{
if(other.tag==“接地”&&hasShoot==true)
{
isDead=true;
Debug.Log(“死”);
DeathZone.enabled=false;
}
}
IEnumerator发布()
{
产生返回新的WaitForSeconds(releaseTime);
GetComponent().enabled=false;
hasShoot=true;
}
}
需要更改行
if (Input.touchCount >= 0 && isDead == false)
到
错误是什么?可能是因为
Input.touchCount
是0
,但您试图访问GetTouch(0)
,至少需要1次触摸!或=1
或!=0
但是是;)
if (Input.touchCount > 0 && isDead == false)