void Update()
        {
            for (int id = 0; id < maxTouches; id++)
            {
                int idAsTouch = -1;

                if (usingMouse && Input.GetMouseButton(0))
                {
                    idAsTouch = 0;
                }
                else
                {
                    for (int i = 0; i < Input.touchCount; i++)
                    {
                        if (Input.GetTouch(i).fingerId == id)
                        {
                            idAsTouch = i;
                        }
                    }
                }

                if (idAsTouch >= 0)
                {
                    if (!mouseDownLastFrame[id])
                    {
                        RaycastHit hitInfo;
                        if (Physics.Raycast(Camera.main.ScreenPointToRay(GetPosition(idAsTouch)), out hitInfo, Mathf.Infinity, pathInputObjectLayer))
                        {
                            if (hitInfo.transform.parent == null)
                            {
                                Debug.LogError(hitInfo.transform.name + " is using the PathInputObject layer, but does not have a parent object with either a PathFollower or a DestinationObject component!");
                                return;
                            }

                            GameObject objectHit = hitInfo.transform.parent.gameObject;

                            timeSwipeStarted[id] = Time.time;
                            initialClickPos[id]  = Camera.main.ScreenToViewportPoint(GetPosition(idAsTouch));

                            if (objectHit.GetComponent <PathFollower>())
                            {
                                objectOnPath[id] = objectHit;

                                PathFollower pf = objectOnPath[id].GetComponent <PathFollower>();
                                if (pf.destObject)
                                {
                                    destObject[id] = pf.destObject;
                                }
                                else
                                {
                                    destObject[id] = (GameObject)Instantiate(destObjectPrefab, objectOnPath[id].transform.position, objectOnPath[id].transform.rotation);
                                    pf.destObject  = destObject[id];
                                    destObject[id].GetComponent <PathDestination>().objectOnPath = objectOnPath[id];

                                    CharacterController destObjCC = destObject[id].GetComponent <CharacterController>();
                                    CharacterController onPathCC  = objectOnPath[id].GetComponent <CharacterController>();
                                    destObjCC.radius = onPathCC.radius;
                                    destObjCC.height = onPathCC.height;
                                    destObjCC.center = onPathCC.center;

                                    destObject[id].transform.Find("Marker").localScale *= destObjCC.radius / 0.5f;
                                }

                                lastPosOnPath[id] = objectOnPath[id].transform.position;
                                isTap[id]         = true;
                            }
                            else if (objectHit.GetComponent <PathDestination>())
                            {
                                objectOnPath[id]  = objectHit.GetComponent <PathDestination>().objectOnPath;
                                destObject[id]    = objectHit;
                                lastPosOnPath[id] = destObject[id].transform.position;
                                isTap[id]         = false;
                            }
                            else
                            {
                                Debug.LogError(objectHit.name + " has a PathInputObject child, but does not have either a PathFollower or a DestinationObject component!");
                            }
                        }
                    }
                    else
                    {
                        if (objectOnPath[id] && destObject[id])
                        {
                            RaycastHit hitInfo;
                            if (Physics.Raycast(Camera.main.ScreenPointToRay(GetPosition(idAsTouch)), out hitInfo, Mathf.Infinity, pathFloorLayer))
                            {
                                float dist     = (Camera.main.ScreenToViewportPoint(GetPosition(idAsTouch)) - initialClickPos[id]).magnitude;
                                float duration = Time.time - timeSwipeStarted[id];

                                if (isTap[id] && (dist > maxTapDistance || duration > maxTapDuration))
                                {
                                    objectOnPath[id].GetComponent <PathFollower>().ClearPath();
                                    objectOnPath[id].GetComponent <PathFollower>().AddPathNode(objectOnPath[id].transform.position);
                                    destObject[id].transform.position = objectOnPath[id].transform.position;
                                    isTap[id] = false;
                                }

                                if (!isTap[id])
                                {
                                    Vector3 oldPos = destObject[id].transform.position;

                                    CharacterController cc         = destObject[id].GetComponent <CharacterController>();
                                    Vector3             moveVector = hitInfo.point - destObject[id].transform.position;
                                    moveVector.y = Mathf.Min(moveVector.y, 0.0f);
                                    cc.Move(moveVector);

                                    if (destObject[id].transform.position.y > oldPos.y)
                                    {
                                        destObject[id].transform.position = oldPos;
                                    }

                                    if (Vector3.Distance(destObject[id].transform.position, lastPosOnPath[id]) > minPathSegmentLength)
                                    {
                                        if (!objectOnPath[id].GetComponent <PathFollower>().AddPathNode(destObject[id].transform.position))
                                        {
                                            objectOnPath[id] = null;
                                            destObject[id]   = null;
                                        }
                                        lastPosOnPath[id] = destObject[id].transform.position;
                                    }
                                }
                            }
                        }
                    }
                    mouseDownLastFrame[id] = true;
                }
                else
                {
                    if (objectOnPath[id])
                    {
                        if (isTap[id])
                        {
                            objectOnPath[id].SendMessage("PathFollowerTapped", SendMessageOptions.DontRequireReceiver);
                        }
                    }

                    objectOnPath[id]       = null;
                    destObject[id]         = null;
                    mouseDownLastFrame[id] = false;
                }
            }
        }
Esempio n. 2
0
 void Start()
 {
     characterController = GetComponent <CharacterController>();
     pathFollower        = GetComponent <PathFollower>();
 }