Xlua移动端控制物体旋转和缩放

--Xlua控制移动模型的旋转和缩放

local firstPosX = 0
local currentRotY = 0
local roleObj = self
local Input = CS.UnityEngine.Input
local TouchPhase = CS.UnityEngine.TouchPhase
local Mathf = CS.UnityEngine.Mathf
local Quaternion = CS.UnityEngine.Quaternion
local Vector2 = CS.UnityEngine.Vector2
local Vector3 = CS.UnityEngine.Vector3

local  oldTouch1=nil
local  oldTouch2=nil
local  zoomMini = 0.5					   	 --限制缩放最小尺寸
local  zoomMax = 2.5						 --限制缩放最大尺寸

local beginSize = Vector3(1,1,1)     		 --模型的初始大小
local beginRotation = Quaternion(0,0,0,0)	 --模型的初始角度


function start()

	beginSize=roleObj.transform.localScale
	beginRotation=roleObj.transform.localRotation

end


function update()

 	if (Input.touchCount == 1) then
 		rotation()
 	end
	
	if (Input.touchCount >= 2)  then
		zoom()
	end

end


--触摸控制模型旋转
function rotation()

  	if (Input.GetTouch(0).phase == TouchPhase.Began) then

  		 firstPosX = Input.GetTouch(0).position.x
  		 currentRotY = roleObj.transform.localRotation.eulerAngles.y
  	end


  	if (Input.GetTouch(0).phase == TouchPhase.Moved) then

  	 	 currentPosX = Input.GetTouch(0).position.x
		 XAxis = firstPosX - currentPosX

		  if (Mathf.Abs(XAxis) >= 5)then

 			 roleObj.transform.localRotation = Quaternion.Euler(Vector3(0, currentRotY + XAxis, 0))

		  end
  	end


  	if (Input.GetTouch(0).phase == TouchPhase.Ended) then

  		firstPosX=0
  		currentRotY = roleObj.transform.localRotation.eulerAngles.y
  	end


end


--触摸控制模型缩放
function zoom()

	newTouch1 = Input.GetTouch(0);
	newTouch2 = Input.GetTouch(1);

	 if (newTouch2.phase == TouchPhase.Began) then

	 	 oldTouch2 = newTouch2
		 oldTouch1 = newTouch1

	 else

	 	 oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position)
		 newDistance = Vector2.Distance(newTouch1.position, newTouch2.position)
		 offset = (newDistance - oldDistance)/350

		 localScale = roleObj.transform.localScale
		 scale = Vector3(localScale.x + offset, localScale.y + offset, localScale.z + offset)

		 roleObj.transform.localScale = Vector3(Mathf.Clamp(scale.x, zoomMini, zoomMax),Mathf.Clamp(scale.y, zoomMini, zoomMax), Mathf.Clamp(scale.z, zoomMini, zoomMax))


		 oldTouch1 = newTouch1
         oldTouch2 = newTouch2
	 end


end


--重置旋转和缩放
function reset()

	roleObj.transform.localScale=beginSize
	roleObj.transform.localRotation=beginRotation

end




 

你可能感兴趣的:(Xlua,xlua,Xlua,热更新,lua)