Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help to Move objects with mouse click

Discussion in 'Scripting' started by gmilitel, Apr 16, 2015.

  1. gmilitel

    gmilitel

    Joined:
    Oct 14, 2014
    Posts:
    20
    Hi, I have the following scene with a script working but I need to make a little change, when I click on the map and the object start moving the box move as is (straight forward), and I need that if the box is facing north and click to south I need the box to rotate and face south. See picture



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class TapToMove : MonoBehaviour {
    4. //flag to check if the user has tapped / clicked.
    5. //Set to true on click. Reset to false on reaching destination
    6. private bool flag = false;
    7. //destination point
    8. private Vector3 endPoint;
    9. //alter this to change the speed of the movement of player / gameobject
    10. public float duration = 50.0f;
    11. //vertical position of the gameobject
    12. private float yAxis;
    13. void Start(){
    14.   //save the y axis value of gameobject
    15.   yAxis = gameObject.transform.position.y;
    16. }
    17.  
    18. // Update is called once per frame
    19. void Update () {
    20.   //check if the screen is touched / clicked
    21.   if((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0)))
    22.   {
    23.    //declare a variable of RaycastHit struct
    24.    RaycastHit hit;
    25.    //Create a Ray on the tapped / clicked position
    26.    Ray ray;
    27.    //for unity editor
    28.    #if UNITY_EDITOR
    29.    ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    30.    //for touch device
    31.    #elif (UNITY_ANDROID || UNITY_IPHONE || UNITY_WP8)
    32.    ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
    33.    #endif
    34.    //Check if the ray hits any collider
    35.    if(Physics.Raycast(ray,out hit))
    36.    {
    37.     //set a flag to indicate to move the gameobject
    38.     flag = true;
    39.     //save the click / tap position
    40.     endPoint = hit.point;
    41.     //as we do not want to change the y axis value based on touch position, reset it to original y axis value
    42.     endPoint.y = yAxis;
    43.     Debug.Log(endPoint);
    44.    }
    45.  
    46.   }
    47.   //check if the flag for movement is true and the current gameobject position is not same as the clicked / tapped position
    48.   if(flag && !Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude)){ //&& !(V3Equal(transform.position, endPoint))){
    49.    //move the gameobject to the desired position
    50.    gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, endPoint, 1/(duration*(Vector3.Distance(gameObject.transform.position, endPoint))));
    51.   }
    52.   //set the movement indicator flag to false if the endPoint and current gameobject position are equal
    53.   else if(flag && Mathf.Approximately(gameObject.transform.position.magnitude, endPoint.magnitude)) {
    54.    flag = false;
    55.    Debug.Log("I am here");
    56.   }
    57.  
    58. }
    59. }
    Any help appreciated.
    Regards.
     
    Last edited: Apr 16, 2015
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  3. gmilitel

    gmilitel

    Joined:
    Oct 14, 2014
    Posts:
    20
    Thanks for the reply, what do i need to change in the code to implement Quaternion.LookRotation?