- Home /
Help change the subject of this Script
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);
}
}
}
Answer by AcePilot10 · Jan 02, 2018 at 09:52 AM
You could make a public GameObject variable for the target object, then drag in the target object from the heirarchy to the script, then on line 38 instead of Instantiate set the target objects position to the target position and rotation to target rotation etc...
$$anonymous$$ay you add and edit this to script yourself, please ? this would be so generous of you