Input.GetTouch 获取触摸

Input.GetTouch 获取触摸

static function GetTouch (index : int) : Touch

Description描述

Returns object representing status of a specific touch (Does not allocate temporary variables).

返回一个存放触摸信息的对象(不允许分配临时变量)。

1.手指移动控制物体移动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class moveCube : MonoBehaviour {
    static int count;//定义touchCount数
    public GameObject particle_;//定义存放cube对象
    public Vector3 touchposition;//存储移动三维坐标值

	// Update is called once per frame
	void Update () {
        if (Input.touchCount > 0)
        {
            count += Input.touchCount;
        }
        if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)) //[color=Red]如果点击手指touch了  并且手指touch的状态为移动的[/color]
        {
            touchposition = Input.GetTouch(0).deltaPosition;  //[color=Red]获取手指touch最后一帧移动的xy轴距离[/color]
            particle_.transform.Translate(touchposition.x*0.01f, touchposition.y*0.01f, 0);//[color=Red]移动这个距离[/color]
        }
    }
    void OnGUI()
    {
        GUI.Label(new Rect(10, 10, 100, 30), "cishu:" + count.ToString());
        GUI.Label(new Rect(10, 50, 100, 30), touchposition.ToString());
    }
}

2.当用户在屏幕点击时实例一个炮弹

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public GameObject projectile;
	void Update() {
		int i = 0;
		while (i < Input.touchCount) {
			if (Input.GetTouch(i).phase == TouchPhase.Began)
				clone = Instantiate(projectile, transform.position, transform.rotation);

			++i;
		}
	}
}

3.当用户在屏幕点击时发射一条光线

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public GameObject particle;
	void Update() {
		int i = 0;
		while (i < Input.touchCount) {
			if (Input.GetTouch(i).phase == TouchPhase.Began) {
				Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
				if (Physics.Raycast(ray))
					Instantiate(particle, transform.position, transform.rotation);

			}
			++i;
		}
	}
}

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值