unity3d instantiate and drag object with mouse
Hello; Could you help me on a topic.
When clicked with a mouse, an object must be created and moved to the position of the mouse in its position.
Age of empires, as in the form of build logic.
My english is not very good, sorry.
Thank you very much for your help.
Comment
Answer by repsacc · Feb 01, 2017 at 02:21 PM
From https://docs.unity3d.com/ScriptReference/Input-mousePosition.html
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public GameObject particle;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
RaycastHit hitInfo;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hitInfo))
Instantiate(particle, hitInfo.point, transform.rotation);
}
}
}
This instantiates an object at the position of the mouse, when "Fire1" is clicked, which is left click on a mouse usually for example.
Your answer
Follow this Question
Related Questions
Unity lookat at finger position 3D C# 0 Answers
create an object at runtime on mouseclick position 0 Answers
Destroy, deactive or hide 0 Answers