Log in or register to post comments

Touch Rotate 3D Object

November 14, 2014 - 3:21pm #1

I need to rotate a Cube with the touch finger. This is the code, but does not work. How can i fix that?

  var touch : Touch;
     if (Input.touchCount > 0) {
         if (Input.GetTouch(0).phase == TouchPhase.Moved){
             var primarytouch : Touch = Input.GetTouch(0);
             var speed = primarytouch.deltaPosition/primarytouch.deltaTime;
             }
             
          if (Input.GetTouch(0).phase == TouchPhase.Ended){
         var finishedtouch : Touch = Input.GetTouch(0);
         var ray : Ray = Camera.main.ScreenPointToRay(finishedtouch.position);
         
         var controller : CharacterController = GetComponent(CharacterController);
         var hit : RaycastHit;
         var moveDirection : Vector3 = Vector3.zero;
        
         if (Physics.Raycast(ray,hit,100))
         {
         var startPos = transform.position;
         var targetPoint = hit.point;
         var targetRotation = Quaternion.LookRotation(targetPoint - transform.position, Vector3.up);
         transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.time *2.0);   
         transform.position = Vector3.Lerp(startPos, hit.point, Time.time * 1);
         
         }   
 }
 }
 

 

Touch Rotate 3D Object

November 17, 2014 - 12:22am #4
if (Physics.Raycast (ray, out hit) && hit.collider.gameObject.name == "MyCube") {

	if (userTouch.phase == TouchPhase.Moved) {
					
		transform.Rotate (0.0f, -userTouch.deltaPosition.x * rotationSpeed, 0, Space.Self); 
	}
}

 

Touch Rotate 3D Object

November 15, 2014 - 12:45pm #3

Does not work!

Touch Rotate 3D Object

November 15, 2014 - 11:50am #2

You can try this: http://answers.unity3d.com/questions/255118/rotating-an-object-with-touch.html

I havent personally tried it myself but hope it somehow works for you.

Log in or register to post comments