PowenKo, Unity Tutorial 145 ,Input, iphone drag and drop

August 31, 2011   ·   0 Comments

ChickenMove.cs


using UnityEngine;
using System.Collections;

public class ChickenMove : MonoBehaviour
{

  bool startTouch;
	public Ray ray;// = Camera.main.ScreenPointToRay(Input.mousePosition);
	Vector3 startTouchPos;

		float hitDistance;
	Vector3 startItemPosition;

	/// ///

	Vector3 screenPos;
	Vector3 offset;
  void Update()
  {

		if (iPhoneInput.touchCount == 1)
		{

            Touch f0 = Input.GetTouch(0);
            Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
            RaycastHit hit;

            if (Physics.Raycast (ray, out hit, 100))
			{
                //Check to see if object hit is been set to moveable
              //  if(hit.collider.tag == "Moveable")
			//	{
                    if (f0.phase == TouchPhase.Began)
					{
                        screenPos = Camera.main.WorldToScreenPoint(transform.position);
                        offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(f0.position.x, f0.position.y, screenPos.z));
                    }else if(f0.phase == TouchPhase.Moved)
					{
                      Vector3  currentScreenPos = new Vector3(f0.position.x, f0.position.y, screenPos.z);
                      Vector3  currentPos = Camera.main.ScreenToWorldPoint(currentScreenPos) + offset;
                        transform.position = currentPos;
					}
             //   }
            }
        }

  }

}

sample code:
b28_drag

shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Unity3D4

Amazon

adsbygoogle

Recent Posts

Categories

Amazon