NEIL的Unity學習筆記(2) - 實例手機上的觸控:旋轉和縮放
本來想做一系列的手機UI的製作,但目前還是騰不出時間來
這一次就說一下觸控,話時在找資料的時間真的讓人覺得中國人好可憐。
基本上你在GOOGLE關鍵字都是COPY 雨松MOMO的程式碼,很多都是直接COPY就算了,什麼都不解釋,外國的還會一直在教你,差遠了。
我這有一半是來自雨松MOMO的,他的範例中並沒有很詳細的對兩點旋轉做運用,但縮放倒是做到很到位,所以我加以修改,完善為可用的手機實例。
請大家跟我做
只在在場中加一個plane和一個cube,然後為CUBE 加上"run"和Main camera加上 "CamControl"即可,程式碼很簡單,還是自己慢慢看吧。
記得要設定run的speed
你想要的camera距離的xyz
希望有誰有更好的做法歡迎提供大家交流,程式是死的,創意才是活的,請不要吝嗇。
------------------------------run------------------------------------------------
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class run : MonoBehaviour {
public int speed;
Animator animator;
Camera cam;
// Use this for initialization
void Start () {
//animator = GetComponent<Animator>();
cam = Camera.main;
}
// Update is called once per frame
void Update () {
if(Input.touchCount ==1){
Ray ray = cam.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
Touch touch = Input.GetTouch(0);
// Handle finger movements based on touch phase.
switch (touch.phase) {
// Record initial touch position.
case TouchPhase.Began:
if (Physics.Raycast(ray, out hit,200)){
transform.LookAt(new Vector3(hit.point.x,1.2f,hit.point.z));
transform.Translate(Vector3.forward*Time.deltaTime*speed);
//animator.Play("run");
}
break;
case TouchPhase.Moved:
if (Physics.Raycast(ray, out hit,200)){
transform.LookAt(new Vector3(hit.point.x,1.2f,hit.point.z));
transform.Translate(Vector3.forward*Time.deltaTime*speed);
//animator.Play("run");
}
break;
case TouchPhase.Stationary:
if (Physics.Raycast(ray, out hit,200)){
transform.LookAt(new Vector3(hit.point.x,1.2f,hit.point.z));
transform.Translate(Vector3.forward*Time.deltaTime*speed);
//animator.Play("run");
}
break;
case TouchPhase.Ended:
// animator.Play("idle");
break;
}
}
}
}
這一次就說一下觸控,話時在找資料的時間真的讓人覺得中國人好可憐。
基本上你在GOOGLE關鍵字都是COPY 雨松MOMO的程式碼,很多都是直接COPY就算了,什麼都不解釋,外國的還會一直在教你,差遠了。
我這有一半是來自雨松MOMO的,他的範例中並沒有很詳細的對兩點旋轉做運用,但縮放倒是做到很到位,所以我加以修改,完善為可用的手機實例。
請大家跟我做
只在在場中加一個plane和一個cube,然後為CUBE 加上"run"和Main camera加上 "CamControl"即可,程式碼很簡單,還是自己慢慢看吧。
記得要設定run的speed
你想要的camera距離的xyz
希望有誰有更好的做法歡迎提供大家交流,程式是死的,創意才是活的,請不要吝嗇。
------------------------------run------------------------------------------------
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class run : MonoBehaviour {
public int speed;
Animator animator;
Camera cam;
// Use this for initialization
void Start () {
//animator = GetComponent<Animator>();
cam = Camera.main;
}
// Update is called once per frame
void Update () {
if(Input.touchCount ==1){
Ray ray = cam.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
Touch touch = Input.GetTouch(0);
// Handle finger movements based on touch phase.
switch (touch.phase) {
// Record initial touch position.
case TouchPhase.Began:
if (Physics.Raycast(ray, out hit,200)){
transform.LookAt(new Vector3(hit.point.x,1.2f,hit.point.z));
transform.Translate(Vector3.forward*Time.deltaTime*speed);
//animator.Play("run");
}
break;
case TouchPhase.Moved:
if (Physics.Raycast(ray, out hit,200)){
transform.LookAt(new Vector3(hit.point.x,1.2f,hit.point.z));
transform.Translate(Vector3.forward*Time.deltaTime*speed);
//animator.Play("run");
}
break;
case TouchPhase.Stationary:
if (Physics.Raycast(ray, out hit,200)){
transform.LookAt(new Vector3(hit.point.x,1.2f,hit.point.z));
transform.Translate(Vector3.forward*Time.deltaTime*speed);
//animator.Play("run");
}
break;
case TouchPhase.Ended:
// animator.Play("idle");
break;
}
}
}
}
--------------------------------------------------------------------------------------
--------------------------------CamControl----------------------------------------------
using UnityEngine;
using System.Collections;
public class CamControl : MonoBehaviour {
public GameObject target;
public float xpos,ypos,zpos;
float distance=1.5f,cam_posy,cam_posz;
Vector2 startPos;
Vector2 direction,oldPosition1,oldPosition2;
Vector3 UpdatePos;
float sum_deltaPos = 0;
int abs=1, state=0;
bool dir=false;
// Use this for initialization
void Start () {
transform.position = new Vector3 (target.transform.position.x,ypos,target.transform.position.z-zpos);
UpdatePos = transform.position;
transform.LookAt(target.transform.position);
}
// Update is called once per frame
void Update () {
if (Input.touchCount > 1) {
if(Input.GetTouch(0).phase==TouchPhase.Moved||Input.GetTouch(1).phase==TouchPhase.Moved){
Touch touch1 = Input.GetTouch(0);
Touch touch2 = Input.GetTouch(1);
if(touch1.deltaPosition.y>0)abs=1;
else abs=-1;
sum_deltaPos = Mathf.Abs(touch1.deltaPosition.x)+Mathf.Abs(touch1.deltaPosition.y);
if(state!=2){//zoom
if(isEnlarge(oldPosition1,oldPosition2,touch1.position,touch2.position)){
if(transform.camera.fieldOfView>30)
transform.camera.fieldOfView-=1;
}
else{
if(transform.camera.fieldOfView<60)
transform.camera.fieldOfView+=1;
}
}
if(state!=1){//rotate
transform.RotateAround(target.transform.position,Vector3.up, sum_deltaPos*abs);
UpdatePos=new Vector3(transform.position.x-target.transform.position.x,0,transform.position.z-target.transform.position.z);
}
oldPosition1=touch1.position;
oldPosition2=touch2.position;
}
}
else{
dir=false;
state=0;
}
}
void LateUpdate () {
float x, z;
x = UpdatePos.x + target.transform.position.x;
z = UpdatePos.z + target.transform.position.z;
transform.position =new Vector3 (x,ypos,z);
}
bool isEnlarge(Vector2 oP1,Vector2 oP2,Vector2 nP1,Vector2 nP2){
//函数传入上一次触摸两点的位置与本次触摸两点的位置计算出用户的手势
float leng1 =Mathf.Sqrt((oP1.x-oP2.x)*(oP1.x-oP2.x)+(oP1.y-oP2.y)*(oP1.y-oP2.y));
float leng2 =Mathf.Sqrt((nP1.x-nP2.x)*(nP1.x-nP2.x)+(nP1.y-nP2.y)*(nP1.y-nP2.y));
if(-1>leng1-leng2){
//放大手势
state=1;
return true;
}else if(1<leng1-leng2){
//缩小手势
state=1;
return false;
}
else{
state=2;
return false;
}
}
}
---------------------------------------------------------------------------------
留言
張貼留言