I have googled and found a script for the following task; Make the camera move to touched position. However, this script will jump to x and y positions on an object, while i would like for it to move to the middle of the object, and then only be able to jump to others.

using UnityEngine;
using System.Collections;

public class TapToMove : MonoBehaviour 
    // flag to check if the user has tapped / clicked.
    // Set to true on click. Reset to false on reaching destination
    private bool flagTouchCoords = false;
    // destination point
    private Vector3 cameraDestination;
    // alter this to change the speed of the movement of player / gameobject
    public float duration = 100.0f;
    // vertical position of the gameobject
    private float yAxis;

    void Start() 
        // save the y axis value of gameobject
        yAxis = gameObject.transform.position.z;

    // Update is called once per frame
    void Update()
        // check if the screen is touched / clicked  
        if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) 
        || (Input.GetMouseButtonDown(0)))
            // declare a variable of RaycastHit struct
            RaycastHit hit;
            // Create a Ray on the tapped / clicked position
            Ray ray;
            // for unity editor
            #if UNITY_EDITOR
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            // for touch device
            #elif (UNITY_ANDROID || UNITY_IPHONE || UNITY_WP8)
            ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);

            // Check if the ray hits any collider
            if (Physics.Raycast(ray, out hit))
                // set a flag to indicate to move the gameobject
                flagTouchCoords = true;
                // save the click / tap position
                cameraDestination = hit.point;
                // as we do not want to change the y axis value based on touch position, reset it to original y axis value
                cameraDestination.z = yAxis;

        // check if the flag for movement is true and the current gameobject position is not same as the clicked / tapped position
        if (flagTouchCoords 
        && !Mathf.Approximately(gameObject.transform.position.magnitude, cameraDestination.magnitude))
            // && !(V3Equal(transform.position, endPoint))){
            // move the gameobject to the desired position
            gameObject.transform.position =
                             1 / (duration * (Vector3.Distance(gameObject.transform.position, cameraDestination))));

enter image description here

So this is how the game looks. The script moves correctly, but will jump at different positions when it hits the collider on the lily pads. I could reduce the collider, but I feel like there should be an easy way to get length and width of a collider, and reduce it by /2 to make the camera move directly in the middle.

Anyone care to help? Thanks in advance. :)


1 Answer 1


if you want to obtain the object owner of the collider instead of the hit point (that can be anywhere over the collider) simply use this code instead of hit.point:

cameraDestination = hit.collider.gameObject.transform.position;

The camera will center on the gameobject that owns the collider. If the position of the object is also the "visual" center you are done.

Hope this is your wanted behaviour.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .