i have a script that instaniate an object for examlpe a “cube” from the project to spawn at the mouse position
i want to change to make the script " move an already exist in the hierarchy to the mouse position
NOTE : “not creating new one of it” “just moving an already exist object in the hirarchy to the mouse position”
i hope i presented my proplem well for anyone wants to help here is the script
using UnityEngine;
using System.Collections;
public class InstantiateAtMousePosition : MonoBehaviour {
Vector3 mousePosition,targetPosition;
//To Instantiate TargetObject at mouse position
public Transform targetObject;
float distance=10f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//To get the current mouse position
mousePosition=Input.mousePosition;
//Convert the mousePosition according to World position
targetPosition=Camera.main.ScreenToWorldPoint(new Vector3(mousePosition.x,mousePosition.y,distance));
//Set the position of targetObject
targetObject.position=targetPosition;
//Debug.Log(mousePosition+" "+targetPosition);
//If Left Button is clicked
if(Input.GetMouseButtonUp(0))
{
//create the instance of targetObject and place it at given position.
Instantiate(targetObject,targetObject.transform.position,targetObject.transform.rotation);
}
}
}