Unity第一人称脚本+触屏

using UnityEngine;
using UnityEngine.UI;

public class MouseLook : MonoBehaviour
{
	//UI窗口
	public GameObject UI = null;
	float speed = 0.2f;
	float x = 0f;
	float y = 0f;
	float minY = -75f;
	float maxY = 75f;
	Touch touch;
	float moveX = 0f;
	float moveY = 0f;

	void Update()
	{
		if (Input.touchCount >= 1)
		{
			touch = Input.GetTouch(0);
		}
		//GetMouseButton(0) 可用于判断touch(0)
		if (Input.GetMouseButton(0))
		{
            #region 判断touch在UI窗口内
			//不用的话注释掉这一段
            moveX = Mathf.Abs(touch.position.x - UI.transform.position.x);
			moveY = Mathf.Abs(touch.position.y - UI.transform.position.y);
			if (moveX >= 480)
			{
				return;
			}
			if (moveY >= 240)
			{
				return;
			}
            #endregion

            x = transform.localEulerAngles.y + touch.deltaPosition.x * speed;
			y += touch.deltaPosition.y * speed;
			//角度限制
			y = Mathf.Clamp(y, minY, maxY);
			transform.localEulerAngles = new Vector3(y * -1f, x, 0);
		}
	}
}

窗口内的触摸用来旋转Camera,UI是一个RawImage贴上RenderTexture,同步了Camera的画面

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值