Instantate on mouse position
Hi guys.
I'm trying to demonstrate Faux Gravity tomorrow and it would be a better example if I could use some cubes so where I click, a cube spawns in the location of my mouse. Note that my faux gravity project uses an orthographic camera and appears 2D, is actually 3D programming/component-wise.
Any further information needed will be replied to as soon as possible.
(Note that I would prefer C# so I can learn how the script functions, I am still getting the grips of Unity and would appreciate C# so I can read it easily).
The following allows you to spawn an object where ever you click your mouse on screen.
using UnityEngine;
using System.Collections;
public class Spawner : $$anonymous$$onoBehaviour
{
// public variable for the desired prefab - it must be set in the inspector
public GameObject prefab;
private int count = 0;
void Start ()
{
if (prefab == null)
{
Debug.LogError("The prefab variable was not set in the inspector. Please set and try again");
}
}
// Update is called once per frame
void Update ()
{
if ((Input.Get$$anonymous$$ouseButtonDown(0)) && // on left mouse button click
(prefab != null))
{
// get the mouse position
Vector3 mousePos = Input.mousePosition;
// set the mouse position to the screen point
mousePos.z = -Camera.main.gameObject.transform.position.z;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
// instantiate the prefab with the mouse position
GameObject newObject = (GameObject) Instantiate(prefab, mousePos, Quaternion.identity);
// Set the name of the newly created object
newObject.name = "Instantiated Object_" + count;
count++;
}
}
}
Add th script above to a game object in your hierarchy. You will need to add the proper components to your prefab (Rigidbody, collider) to get gravity. If you have created a floor/ground in your game make sure either there is no Rigidbody on the floor/ground or that Is $$anonymous$$inematic
is checked.
@$$anonymous$$avina I appreciate the help however I still seem to be having a similar problem as I did with my previous attempts. It appears that when I click, though the cube does Instantiantiate, it appears off camera in a very off position. This may be because of my orthographic camera.
Anyways, appreciate the help :)!
Normally an orthographic camera has a -10 Z position, but that should not matter as I have tested this for both orthographic and perspective cameras. There could be something else in your project that is preventing this from working correctly.
Here is a package that spawns cube prefabs (the prefab can be anything) at the mouse position clicked (the camera is orthographic). This little demo not only spawns the prefab but the items spawned are affected by gravity.
Your answer
Follow this Question
Related Questions
Object reference not set message, directly after instantiation 1 Answer
How can I increase the speed gradually of each instantiated clone. 0 Answers
RPG instantiate problem 3 Answers
Question About Unity Class Hierarchy 3 Answers
How to instantiate a game object every time when space key is pressed 2 Answers